Similar to this question, how do I set a property on the join model just before save in this context?

class Post < ActiveRecord::Base
  has_many :post_assets
  has_many :assets, :through => :post_assets
  has_many :featured_images, :through => :post_assets, :class_name => "Asset", :source => :asset, :conditions => ['post_assets.context = ?', "featured"]

end

class PostAssets < ActiveRecord::Base
  belongs_to :post
  belongs_to :asset

  # context is so we know the scope or role
  # the join plays
  validates_presences_of :context
end

class Asset < ActiveRecord::Base
  has_many :post_assets
  has_many :posts, :through => :post_assets
end

I just want to be able to do this:

@post = Post.create!(:title => "A Post")
@post.featured_images << Asset.create!(:title => "An Asset")
# ...
@post = Post.first
@featured = @post.featured_images.first
  #=> #<Asset id: 1, title: "An Asset">
@featured.current_post_asset #=> #<PostAsset id: 1, context: "featured">

How would that work? I've been banging my head over it all day :).

What currently happens is when I do this:

@post.featured_images << Asset.create!(:title => "An Asset")

Then the join model PostAsset that gets created never gets a chance to set context. How do I set that context property? It looks like this:

PostAsset.first #=> #<PostAsset id: 1, context: nil>

Update:

I have created a test gem to try to isolate the problem. Is there an easier way to do this?!

This ruby-on-rails associations join

Edit
Report