How to represent join tables with Rails 2.3 nested attributes?


#1

I have a model Store with a relationship Address. Because multiple
models have relationships to Address, I have a addresses_stores join
table with a corresponding StoreAddress class that contains
information specific to that store/address (store hours, phone #,
etc).

This seemed like the way to go when I was designing the DB, but now
that I’m dealing with Rails’ nested_attributes, it doesn’t look like
it’s going to work. I’m trying to link each StoreAddress to its
corresponding Address like so:

http://pastie.org/420843

You can see that on line 19, I’m calling fields_for(:store_addresses)
and passing a StoreAddress that should be linked to a corresponding
Address. The problem is, I don’t know how to link the StoreAddress to
the Address if the Address itself is a new record. Even if this were
to work, I’m not sure exactly what params would have to look like so
that StoreAddress objects were linked with corresponding Address
objects, considering the fact that multiple addresses can be submitted
in the form.

So, first question is, what does params have to look like so that I
can submit StoreAddress objects right alongside Address objects in a
Store form. Second question is, how do I get my form to submit those
params.

Any suggestions would be great. :slight_smile: