How to work with has_many :through

I’m trying to understand how to use the has_many :through construct.

I have two tables: customers and vendors associated through
“assignments”
table.

Class Customer
has_many :assignments
has_ many :vendors, :through => :assignments
. . . # other fields
end
Class Vendor
has_many :assignments
has_many :customers, :through => :assignments
. . . # other fields
end

Class Assignment
belongs_to :customer
belongs_to :vendor
. . . # other fields
end

How do I build assignments? By that, I mean assign a vendor to a
customer?
I’m thinking that I should be able to do this:

v = Vendor.find(1)
c = Customer.find(1)
v.customers << c

but, that doesn’t work. Would someone help me with the correct syntax?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs