David Kjelkerud
2006-Jul-13 13:30 UTC
[Rails] Adding attributes to many through associations
Hi, I can''t figure out how to add the additional attributes in my join table. I''m having a through-association between two models (Machine and Service). Each machine can have a certain number of services and a service can belong to several machines. When I create machines, I define which ones out of all services can be applied to this particular machine. Every chosen service should have several attributes (prices in this case) which will be different for each machine. Here''s my models: class Machine < ActiveRecord::Base has_many :machine_service_items has_many :services, :through => :machine_service_items end class Service < ActiveRecord::Base has_many :machine_service_items has_many :machines, :through => :machine_service_items end class MachineServiceItem < ActiveRecord::Base belongs_to :machine belongs_to :service end In my add_machine view I list all the services and under them textfields to fill in the additional attributes for each of the services. What I find difficult is how to create many objects of MachineServiceItem from this form and also adding the right service id. I "think" I want params filled with arrays so I can create all the objects of MachineServiceItem in my controller but I don''t know how to get it. In my head the controller looks something like this: def create for machineserviceitem in params([:machineserviceitems]) Machine.create(params([machineserviceitem]) end end Maybe... Really need some help on this one, been stuck here for some time now! :) -- Posted via http://www.ruby-forum.com/.