Associations give unexpected result during rspec test

hello group,

I recently started with the pragmatic bookshelve rspec book. In one of
the latest chapters, there is an example based on the models
‘message’ and ‘user’.

A message has a recipient and an sender. Users have an association
‘sent_messages’ and ‘received_messages’. After the example the book
invites to add a ‘subscription’ model. A user has one subscription and
it should be possible via the subscription to limit the number of
messages a user can send.

I tried to extend the working rspec examples by adding the
subscription model, but the examples showed me that the associations I
created were not working as I expected. I hope someone can explain me
why.

The code of the models is:

class User < ActiveRecord::Base
has_many( :received_messages, {:class_name => Message.name,
:foreign_key => ‘recipient_id’})
has_many( :sent_messages, { :class_name => Message.name,
:foreign_key => ‘sender_id’ })
belongs_to :subscription, :class_name => ‘Subscription’ # I added
this myself

def send_message( attrs)
    if subscription.can_send_message?
        sent_messages.create! attrs
    end
end

end

class Message < ActiveRecord::Base
belongs_to :recipient, :class_name => ‘User’
validates_presence_of :title, :text, :recipient
end

class Subscription < ActiveRecord::Base # I added this class myself
has_one( :user, { :class_name => User.name,
:foreign_key => ‘subscription_id’ })

def can_send_message? ()
    puts "size =#{user.sent_messages.size}"     # strange. TODO
    user.sent_messages.length < month_limit
end

end

Now, the example I made to test it is not working: if I access the
sent_messages association via the user directly, I get the expected
messages, but if I do it via the user of the subscription, the
association is an empty array.

To show it, I put some print statements in the example. I’m very much
confused. Who can explain what I did wrong?

describe Subscription do
describe “#can_send_message?” do
before( :each) do
@ruud = User.create! :subscription => Subscription.create!
(
:month_limit => 1),
:login => ‘rudd’
@erik = User.create!
end

    context "when a user has reached the subscription limit for

the month" do
it “returns false” do
msg = @ruud.send_message(
:title => “Book Update”,
:text => “Beta 11 includes great
stuff!”,
:recipient => @erik)
p @ruud # << this is the test user
p @ruud.sent_messages
user_via_subscr = @ruud.subscription.user
p user_via_subscr # << the test user, accessed
via the subscription
p user_via_subscr.sent_messages
@ruud.zent_messages.should == [msg]
@ruud.subscription.can_send_message?.should == false
end
end
end
end

The output is

size =0
.size =0
#<User id: 1, login: “rudd”, created_at: “2012-01-02 10:40:32”,
updated_at: “2012-01-02 10:40:32”, subscription_id: 1>
[#<Message id: 1, title: “Book Update”, text: “Beta 11 includes great
stuff!”, recipient_id: 2, created_at: “2012-01-02 10:40:32”,
updated_at: “2012-01-02 10:40:32”, sender_id: 1>]
#<User id: 1, login: “rudd”, created_at: “2012-01-02 10:40:32”,
updated_at: “2012-01-02 10:40:32”, subscription_id: 1>
[]
size =0

As you can see, if I access the user directly or I access the user via
the subscription, p prints the same fields. But the sent_messages
array is different in both cases…

thanks in advance!

Ruud