Reputation: 8982
I am doing this:
@person.attributes = params[:person]
Can anyone give me a clue as to why doing an attributes= would cause a bunch of SQL to show in my logs?
I see a couple of SELECT and even an UPDATE.
I swear I am not doing a "save" -- so I have no idea why setting attributes would trigger queries.
Is there a before_ of after_ filter I am missing?
Thanks.
Upvotes: 1
Views: 1008
Reputation: 107718
Setting attributes on a standard model does not cause any SELECT
or UPDATE
calls as far as I am aware. This is further seen to be true when I run log concurrently with a script/console session:
>> f = Forum.first
==> ./log/development.log <==
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
Forum Load (0.4ms) SELECT * FROM `forums` ORDER BY title asc LIMIT 1
Forum Columns (12.6ms) SHOW FIELDS FROM `forums`
=> #<Forum id: 1, title: "Welcome to rBoard!", description: "This is an example forum for Rboard.", is_visible_to_id: nil, topics_created_by_id: nil, position: 1, parent_id: nil, last_post_id: 1, last_post_forum_id: nil, topics_count: 1, posts_count: 4, category_id: nil, active: true, open: true>
>> f.attributes
=> {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
>> f.attributes = _
=> {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
You can see here that there is only two SQL queries that get executed, one to fetch the Forum
record and the other to find out what columns are on the forums
table.
It is not until I save it that it does some queries:
>> f.attributes
=> {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
>> attr = _
=> {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
>> attr["position"] = 2
=> 2
>> f.save
SQL (0.2ms) BEGIN
=> true
>> SQL (0.2ms) COMMIT
f.attributes = attr
=> {"position"=>2, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
>> WARNING: Can't mass-assign these protected attributes: id
f.save
SQL (0.2ms) BEGIN
Forum Update (20.3ms) UPDATE `forums` SET `position` = 2 WHERE `id` = 1
SQL (27.2ms) COMMIT
=> true
Upvotes: 3