do i need associations to use fields_for

29 views
Skip to first unread message

fugee ohu

unread,
Feb 16, 2020, 10:41:28 PM2/16/20
to Ruby on Rails: Talk
Can I use fields_for for any models or do they have to be associated?

Walter Lee Davis

unread,
Feb 17, 2020, 12:38:03 AM2/17/20
to rubyonra...@googlegroups.com
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.

Walter

> On Feb 16, 2020, at 10:41 PM, fugee ohu <fuge...@gmail.com> wrote:
>
> Can I use fields_for for any models or do they have to be associated?
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/09881329-3137-4c90-94a1-6495bccc90bd%40googlegroups.com.

Message has been deleted

fugee ohu

unread,
Feb 17, 2020, 2:44:41 AM2/17/20
to Ruby on Rails: Talk


On Monday, February 17, 2020 at 12:38:03 AM UTC-5, Walter Lee Davis wrote:
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.

Walter

> On Feb 16, 2020, at 10:41 PM, fugee ohu <fuge...@gmail.com> wrote:
>
> Can I use fields_for for any models or do they have to be associated?
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.

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

Nicholas Schwaderer

unread,
Feb 17, 2020, 4:19:06 AM2/17/20
to rubyonra...@googlegroups.com
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.


Nick Schwaderer

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f2ba1d66-8420-41e9-b228-2456080f0ffb%40googlegroups.com.

Ariel Juodziukynas

unread,
Feb 17, 2020, 7:15:47 AM2/17/20
to rubyonra...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages