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.

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

Really interesting syntax. Can’t say it’s intuitive, but definitely useful. Do we have alternatives?

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

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)