I’m still having a hard time wrapping my brain around many to many
relationships in rails. I understand what they are, and I’ve read all
kinds of code that explains how to update them after finding a specific
id. However, I can’t seem to figure out how to update a many to many
model from a form. I just can’t imagine what the syntax might be.
Here are my models:
class Item < ActiveRecord::Base
has_and_belongs_to_many :print_jobs
end
class PrintJob < ActiveRecord::Base
has_and_belongs_to_many :items
end
I have a table called “print_jobs_items” that contains the following
fields:
print_job_id
item_id
quantity
dimensions
… I won’t list all the fields, but there are about 15.
I have an ajax form that points to the method “save_item_pj_details”.
The form object is called “items_print_job”. What do I actually put in
that method to get rails to save the data from that form to the
database, and add a new record if necessary. Do I have to manually
create database objects for each table, or can I simply feed the form
object to the model just as I normally would with the .new method?
I just (obviously) don’t have a clear picture of how to go about this,
nor can I find any solid examples. Thanks very much.