I have the following three models (massively simplified):
class A < ActiveRecord::Base
has_many :bs
has_many :cs, :through => :bs
end
class B < ActiveRecord::Base
belongs_to :a
has_many :cs
end
class C < ActiveRecord::Base
belongs_to :b
end
It seems that A.cs gets cached the first time it is used (per object) when I'd really rather it not.
Here's a console session that highlights the problem (the fluff has been edited out)
First, the way it should work
rails console
001 > b = B.create
002 > c = C.new
003 > c.b = b
004 > c.save
005 > a = A.create
006 > a.bs << b
007 > a.cs
=> [#<C id: 1, b_id: 1>]
This is indeed as you would expect. The a.cs is going nicely through the a.bs relation.
And now for the caching infuriations
008 > a2 = A.create
009 > a2.cs
=> []
010 > a2.bs << b
011 > a2.cs
=> []
So the first call to a2.cs (resulting in a db query) quite correctly returned no Cs. The second call, however, shows a distinct lack of Cs even though they jolly well should be there (no db queries occurred).
And just to test my sanity is not to blame
012 > A.find(a2.id).cs
=> [#<C id: 1, b_id: 1>]
Again, a db query was performed to get both the A record and the associated C's.
So, back to the question: How do I force rails to not use the cached result? I could of course resign myself to doing this workaround (as shown in console step 12), but since that would result in an extra two queries when only one is necessary, I'd rather not.