Search code examples
ruby-on-railsformshas-onecocoon-gem

Does it make sense to use cocoon solely for a has_one relationship?


I've been trying to get cocoon working with a form that handles a has_one relationship but no has_many relationships, and come across a few gotchas, such as Cocoon and has_one association .

I'm starting to wonder if I'm taking the wrong approach by using cocoon in this scenario. One reason is that the documentation about cocoon talks about has_many relationships, but not has_one ones. Maybe it's because the only benefits for cocoon is when there's a has_many relationship and you want to dynamically create related objects.

Is cocoon supposed to make relationships easier in a way that's useful for has_one relationships?


Solution

  • In my opinion cocoon is not useful for the has_one relationship. You will note that when we create fields_for for some association then we will have the first field generated by default. So now if you have has_many relation and you need to add more of it then you may use cocoon. For has_one relationship normal rails form will do the trick.

    Coocon doesn't make relationships easier rather adding multiple assoicated objects at the same time from a form is made easier using that.

    Hope this helps.