2010-03-22 2 views
1

Я делаю это:Rails ActiveRecord атрибуты =

@person.attributes = params[:person] 

Может кто-нибудь дать мне ключ, почему делающие атрибуты = вызовет кучу SQL, чтобы показать в моих журналах?

Я вижу пару SELECT и даже UPDATE.

Клянусь, я не делаю «сохранить», поэтому я понятия не имею, почему атрибуты атрибутов будут вызывать запросы.

Есть ли предыдущий_ фильтр после меня отсутствует?

Спасибо.

+0

Есть ли у вас поведение, которое срабатывает на уступки? –

ответ

3

Установка атрибутов на стандартной модели не вызывает никаких ошибок SELECT или UPDATE звонит, насколько мне известно. Это видно далее, чтобы быть правдой, когда я запускаю войти одновременно с/консоли сессии в сценария:

>> 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} 

Вы можете увидеть здесь, что есть только два запросов SQL, которые получают выполняются, один для извлечения Forum записи и другой, чтобы узнать, какие столбцы находятся на столе forums.

Это пока я не сохранить его, что он делает некоторые запросы:

>> 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 
+0

Спасибо, Райан. Я узнал, что это связано с тем, что у нас есть пользовательские расширения, которые перезаписывают базовые атрибуты = метод. :(Но спасибо за ваш ответ. – vinhboy

Смежные вопросы