Forms with associations and collections

Hi

Does anyone have a good pattern for using a sinlge form for a model
with multiple associations including some has_many’s?

For example I have a Property model with a has_one association an
Address model and a has_many association to a Highlight model. I would
like to update these all on the same page however if I render the main
Property form, then use a partial for the address and Highlight (with
ajax for adding multiple highlights), I get an issue where to make
things work I have to:

  1. Create a new Address for the Property.address.
  2. If the Address fields are blank the Address record will still be
    created.
  3. So, I use a custom function to test each value of the hash and
    return true if all are blank - only add if false… (i.e.
    @property.build_address(params[:address]) unless
    params[:address].all_values_are_blank?)
  4. If I am updating the property rather than creating it I have to test
    whether the address is nil and either update, create or delete
    depending on what has been passed in params.

Seems a bit messy and rails always seems to have an elegant solution
for this standard sort of stuff so I am sure I am missing something!

Any suggestions for a cleaner why to go about this?

Thanks
Dave

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