I have not see a way to directly access the through record when creating a new has_many :through association. It would seem like I have overlooked something, but in the meantime I created a patch to allow for it. http://dev.rubyonrails.org/attachment/ticket/8478 http://dev.rubyonrails.org/attachment/ticket/8478/adding_to_has_many_through_yields_the_created_through_association.diff Example: class Post < ActiveRecord::Base has_many :tags has_many :taggings, :through => :tags end Post.find(:first).tags << Tag.find(:first) do |through_association| ... # through_association is the created Tagging record end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Mislav Marohnić
2007-Sep-18 00:07 UTC
Re: has_many :through and access to the the through record
On 9/17/07, foomanchoo <johngweir@gmail.com> wrote:> > > Post.find(:first).tags << Tag.find(:first) do |through_association| > ... # through_association is the created Tagging record > endReally interesting syntax. Can''t say it''s intuitive, but definitely useful. Do we have alternatives? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Koziarski
2007-Sep-19 09:14 UTC
Re: has_many :through and access to the the through record
> I have not see a way to directly access the through record when > creating a new has_many :through association. It would seem like I > have overlooked something, but in the meantime I created a patch to > allow for it.The :through part of those associations is really just an optimisation to avoid having to write: @tag.taggings.map(&:post) If you need access to the tagging instances, just use @tag.taggings instead of @tag.posts> http://dev.rubyonrails.org/attachment/ticket/8478 > http://dev.rubyonrails.org/attachment/ticket/8478/adding_to_has_many_through_yields_the_created_through_association.diff > > Example: > > class Post < ActiveRecord::Base > has_many :tags > has_many :taggings, :through => :tags > end > > Post.find(:first).tags << Tag.find(:first) do |through_association| > ... # through_association is the created Tagging record > end > > > > >-- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
foomanchoo
2007-Sep-19 21:13 UTC
Re: has_many :through and access to the the through record
> The :through part of those associations is really just an optimisation > to avoid having to write: > > @tag.taggings.map(&:post) > > If you need access to the tagging instances, just use @tag.taggings > instead of @tag.postsTrue, but I don''t see how this helps in referencing the newly created association. where is the Tagging record in the below? post = Post.find(:first).tags << Tag.find(:first) I would like a reliable and easy method of referencing the created association, which the patch provides and does so without presenting any compatibility issues. However there might be another way, which I have not thought of. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Brandon Keepers
2007-Sep-20 01:43 UTC
Re: has_many :through and access to the the through record
On Sep 19, 2007, at 5:13 PM, foomanchoo wrote:> >> The :through part of those associations is really just an >> optimisation >> to avoid having to write: >> >> @tag.taggings.map(&:post) >> >> If you need access to the tagging instances, just use @tag.taggings >> instead of @tag.posts > > True, but I don''t see how this helps in referencing the newly created > association. > > where is the Tagging record in the below? > post = Post.find(:first).tags << Tag.find(:first) > > I would like a reliable and easy method of referencing the created > association, which the patch provides and does so without presenting > any compatibility issues. However there might be another way, which I > have not thought of.What''s wrong with this: tagging = Post.find(:first).taggings.create :tag => Tag.find(:first) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---