Notice that the second time I invoke the association via user, the object_id remains the same. The related object has been cached. However, passing true to the accessor reloads the relationship and I get a new instance.
>> ts = Timesheet.find :first
=> #<Timesheet:0x3454554 @attributes={“updated_at”=>”2006-11-21
05:44:09”, “id”=>”3”, “user_id”=>”1”, “submitted”=>nil,
“created_at”=>”2006-11-21 05:44:09”}>
>> ts.user.object_id
=> 27421330
>> ts.user.object_id
=> 27421330
>> ts.user(true).object_id
=> 27396270
>> ts = Timesheet.find :first
=> #<Timesheet:0x3454554 @attributes={“updated_at”=>”2006-11-21
05:44:09”, “id”=>”3”, “user_id”=>”1”, “submitted”=>nil,
“created_at”=>”2006-11-21 05:44:09”}>
>> ts.user.object_id
=> 27421330
>> ts.user.object_id
=> 27421330
>> ts.user(true).object_id
=> 27396270
本文探讨了在Ruby on Rails应用中使用缓存机制时,如何处理对象的重复加载问题。通过实例展示了当通过关联调用同一对象时,Rails如何避免重复加载相同的数据,以及如何强制重新加载数据以获取最新状态。
2580

被折叠的 条评论
为什么被折叠?



