Can I use fields_for for any models or do they have to be associated?
You may use fields_for for any object that you have declared "accepts_nested_attributes_for" in your form's parent model. It obviously makes the most sense to do this with a related object.
In my app users have many artists, artists have many tourdates, artists have many venues, users have many venues through artists, tour_dates when added have to provide a venue so they can select from clubs or festivals that are included in the app, and they can also create a user defined venue, or select from previous user defined venues I had this working before I started concerning myself with associations by prepending all the venue fields with venue so state and city become venue_state and venue_city and I add an attribute_accessor to the tour_dates model for each venue field I feel like that still the way to do it, as long as the user defined venues belong to the artist and user so if the user ever deletes the artist the venue will be deleted also
Hello again Fugee,
Fields in forms in Rails do not necessarily have to correspond with model attributes.
Generally, you might find the Rails edge guide on forms very helpful- as well as the
form_with API docs which are a bit more specific for some of these questions.
accepts_nested_attributes_for explicitly checks for associations to exist https://github.com/rails/rails/blob/f33d52c95217212cbacc8d5e44b5a8e3cdc6f5b3/activerecord/lib/active_record/nested_attributes.rb#L340
fields_for does not require accepts_nested_attributes_for to be defined, but it won’t behave the same without that, you can inspect the generated html on both cases