Rails 5: Nested Forms and Existing Related Objects

I am relatively new to Rails, so please forgive me if this proves to be a rather naive question .:]

I have two models that can be tagged: Collection and Video . I support this with the Tag model with polymorphic association and has_many :through association with Tagging . This all works great.

What I find difficult to determine is how to set up the views for this. I know that if I only created or updated tags that are uniquely associated with one model, I could use a simple form and add accepts_nested_attributes_for to my model, but the problem I am facing is that I want to check first tag is present and only create it if it does not exist, otherwise just attach the existing tag to a new video or collection. Obviously, I could do this manually in the controller, but since Rails provides a mechanism for creating and updating related objects automatically, I wondered if it provided something like this to link existing objects?

Also, I'm not sure if I should embed the tags resource and process it centrally in the TagsController , or should I manage the tags from an object management object like VideosController .

Any help or advice is appreciated.

+1
javascript ruby ruby-on-rails activerecord forms
Jun 02 '16 at 2:43 on
source share
1 answer

you need to do it manually, it is not much code.

Similar question

Basically, you'll need find_or_create_by in your controller ( doc ) and fields_for ( doc ) in your collection and video views.

0
Aug 14 '16 at 19:11
source share



All Articles