开发者

Why updating an attribute in model causes to query another table?

Given:

message = Message.find(1)

When I do:

message.update_attributes(:created_at => ...)

I see in the log:

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)

Why does it queries for users with id=5 ?

Some relevant code:

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


It appears as though you may have some sort of before filter set up. These are often used to carry out actions / checks before a method is run - such as checking that a user is logged in.

http://guides.rubyonrails.org/action_controller_overview.html#filters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜