stubbing delegates
I stubbed the subset billed? method
subset.stub(:billed?).and_return(true)line_item has a delegate for billed? to subset billed?
when I call the methods the following occurs
(rdb:1) subset.billed?
true
(rdb:1) subset.line_items[0].billed?
false
(rdb:1) subset === subset.line_items[0].order_subset
true
(rdb:1) subset.billed? == subset.line_items[0].subset.billed?
false
on the first call it works
on the second I call the billed method over the relation delegated and the stub failes on the third I check if the subset and the subest of the line_item-rela开发者_如何转开发tion are the same model and its true on the fourth I compare the output of the same method called directly on the subset and indirectly over the relation and it failsis there anybody ever had this?
subset and subset.line_items[0].order_subset are indeed different objects. === with two objects ends up using the following logic:
http://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L1906-1910
To see what I mean, run this:
subset.object_id == subset.line_items[0].order_subset.object_id
You'll see that you get false.
This is somewhat of a problem with activerecord. When you have reverse associations, rather than referring to the model you've already loaded, it loads an entirely new copy of the record.
精彩评论