has_many :through and with_scope protected

If you were following the excellent has_many :through blog, you may find you can’t use with_scope in the methods defined on your association. Instead, just send :with_scope along.

has_many :active_campaigns, :through => :campaign_distributions do
  def <<(active_campaign)
    CampaignDistribution.send(:with_scope, :create => {}) {
      self.concat active_campaign
    }
  end
end

One Comment

  1. Posted 3/24/2011 at 1:07 pm | Permalink

    Hello,

    I tried to use this method. But, I can’t set the additional attribute in my join table.

    Have you been able to make this work?

Post a Comment

Your email is never shared. Required fields are marked *

*
*