给定:
message = Message.find(1)
当我这样做时:
message.update_attributes(:created_at => ...)
我在日志中看到:
SELECT `users`.* FROM `users` WHERE (`users`.`id` = 5) LIMIT 1
UPDATE `messages` SET `created_at` = '2011-08-30 12:41:01', `updated_at` = '2011-08-30 12:41:03' WHERE (`messages`.`id` = 1)
为什么它会查询具有id=5
的用户?
一些相关代码:
class CreateMessages < ActiveRecord::Migration
def self.up
create_table :messages do |t|
t.integer "publisher_id", :null => false
...
t.timestamps
end
add_index("messages", "publisher_id")
execute "ALTER TABLE messages ADD FOREIGN KEY (publisher_id) REFERENCES users(id)"
end
def self.down
drop_table :messages
end
end
看起来好像您可能已经设置了某种预过滤器。这些通常用于在方法运行之前执行操作/检查,例如检查用户是否登录
http://guides.rubyonrails.org/action_controller_overview.html#filters