I have two models here, courses and studyunits, where a studyunit has content for a course.
Here are the two models:
class Studyunit < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :courses
class Course < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :studyunits
The issue is that adding studyunits to courses seems to update the course. studyunits attribute, but not studyunit.courses. Excerpt of the rspec code:
before(:each) do
course.studyunits << studyunit
Studyunit.connection.clear_query_cache
end
it "should be associated with a course" do
course.studyunits.first.should_not eql(nil)
studyunit.courses.first.should_not eql(nil)
end
The first condition passes, the second fails. How to solve this? I need to access both sides in my code. I tried clearing the query cache as per this thread, but it didn’t solve the issue.
Are you sure this doesn’t just have to do with caching? I suspect it does. Try this code instead: