I have the following model:
class Bill . . . some fields . . . belongs_to :sponsor, :class_name => "Legislator" end class Legislator .. .some fields . . . has_many :bills end
I get this strange behavior, but I am sure this is something simple:
Loading development environment (Rails 3.0.7) b = Bill.first l = Legislator.first l.bills << b l.save => true (I can view l.bills, but l.bills.all.to_a.count is 0) l.govtrack_id => 400001 ruby-1.9.2-p180 :007 > Legislator.where(govtrack_id: 400001).first.bills => []
So I can create the association and view it. The save is successful, but when I retrieve the object, the association is gone . . . no errors. I’m confused, what am I missing?
You’re missing
inverse_ofon yourLegislatormodel. I ran a quick test (to make sure there wasn’t a Mongoid issue). My models were thus:And console output from the test: