1
@person.attributes = params[:person]
誰能給我一個線索,爲什麼做的屬性=會引起一串SQL中我的日誌顯示?
我看到幾個SELECT甚至更新。
我發誓我沒做一個「保存」 - 所以我不知道爲什麼設置屬性會觸發查詢。
我是否缺少after_ filter?
謝謝。
@person.attributes = params[:person]
誰能給我一個線索,爲什麼做的屬性=會引起一串SQL中我的日誌顯示?
我看到幾個SELECT甚至更新。
我發誓我沒做一個「保存」 - 所以我不知道爲什麼設置屬性會觸發查詢。
我是否缺少after_ filter?
謝謝。
就我所知,在標準模型上設置屬性不會導致任何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
謝謝瑞恩。我發現這是因爲我們有覆蓋基本屬性=方法的自定義擴展。 :(但是謝謝你的深入解答。 – vinhboy 2010-03-22 14:15:23
您是否有對轉讓引發的行爲? – 2010-03-22 14:16:54