Olivier Lance
2011-Aug-17 14:14 UTC
has_many :through, collection<<(object) and duplicates.
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!
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Olivier Lance
2011-Aug-24 16:46 UTC
Re: has_many :through, collection<<(object) and duplicates.
Nobody? :\ -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Maybe Matching Threads
- Problem with has_many :through, :uniq => true with polymorph
- polymorphic,has_many through can not work?
- How do you test a module that extends ActiveSupport::Concern?
- Per activerecord object callbacks (aka: should I have gone to bed hours ago?)
- How to use polymorphic in a beautiful way in such a case?