Hi,
I’m facing a strange behavior in Rails with the has_many association and
the generated collection<<(object, …) method that comes along.
I’ve got an Object model and a Tag model, plus a taggings table to join
the two together.
On Object, I set these associations:
has_many :taggings, :as => :taggable, :dependent => :destroy
has_many :tags, :through => :taggings, :uniq => false
(:uniq => false is the default value, I’ve left it to insist on that
point)
Now let’s say I create a new Object and save it.
In an after_save callback on Object, I add a few tags:
self.tags << Tag.find_or_initialize_by_name("hey")
self.tags << Tag.find_or_initialize_by_name("ho")
self.tags << Tag.find_or_initialize_by_name("hey")
So, twice the same tag “hey” and another one “ho”. Nothing fancy.
Once this is done though, I’ll get this:
puts self.tags.inspect # [ "hey", "ho" ]
puts self.tags(true).inspect # [ "hey", "ho", "hey" ]
I don’t get why I don’t see all the three tags at first?!!
Because the second line works, I assume my model needs to be reloaded
from DB, but why would it show any tag in the first call then?
Am I missing something?
Thanks for your help!