I have Orders which has many LineItems. LineItems has_many Leads. Leads aren’t associated to a User until a User purchases them. The association is setup through a HABTM relationship and a join table LeadsUsers.
Once a User purchases a lead I need to setup the association. I have a LeadsUsers model with the right HABTM code setup. In my order model I have the following:
has_many :line_items
after_save :assign_lead_to_user
def assign_lead_to_user
self.line_items.each do
leads_users = LeadsUsers.create :user_id => :user_id, :lead_id => line_item.lead.id
leads_users.save
end
end
This method fails: undefined local variable or method `line_item’. I know this means that it doesn’t know what line_item I am referring to… Any ideas? Ultimately I want to be able to reference User.leads.all.
After a short look, you need to provide a block variable: