[rspec-users] stub_model() and ActiveRecord Associations

Zach Dennis zach.dennis at gmail.com
Wed Aug 27 08:22:55 EDT 2008

On Wed, Aug 27, 2008 at 3:34 AM, Matt Wynne <matt at mattwynne.net> wrote:
>> Here's the basic deal:
>> Model.find(1).equal?(Model.find(1))
>> => false
>> AR does not cache objects, so when you ask it for what you *think*
>> might the same object twice, you get different ones.
> I thought as much... So does AR just cache the object's attributes instead
> and construct them on the fly as and when you ask for them?

It caches the SQL statements and their results. It uses the cached
results to build an instance of your model. Although the identify of
the objects are different, they are equal.

    f = Foo.create :name => "blah"
    f.equal?(Foo.last) # false
    f == Foo.last       # true

I don't know your ultimate goal, but you could rely on object equality
(and not identify equality) in your test:

   @target_comment.should == @target_comments.first

And then rely on @target_comments.first to set up your expectation for

Zach Dennis

More information about the rspec-users mailing list