简体   繁体   English

Rails activerecord属性=

[英]Rails activerecord attributes=

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? 谁能给我一个线索,为什么执行attribute =会导致一堆SQL显示在我的日志中?

I see a couple of SELECT and even an UPDATE. 我看到几个SELECT甚至一个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? 我缺少一个after_ after_过滤器吗?

Thanks. 谢谢。

Setting attributes on a standard model does not cause any SELECT or UPDATE calls as far as I am aware. 据我所知,在标准模型上设置属性不会导致任何SELECTUPDATE调用。 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. 您可以在这里看到只有两个SQL查询被执行,一个用于获取Forum记录,另一个用于查找forums表中的列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM