nested_attributes in Form Helper - what is the "Rails way"?

50 views
Skip to first unread message

Piotr Brych

unread,
Apr 19, 2016, 11:09:21 AM4/19/16
to Ruby on Rails: Talk

We are working with nested attributes and we've reached a point where we're not sure if there is a better, clearer, "more rails" way of dealing with it.

We have 3 models - Organisation, Category and a strictly join table CategoryOrganisation (no additional attributes). The relation between Organisation and Category is many-to-many.

Now, we want to create a form with checkboxes through which the categories for organisations can be added and removed. The example form would look like that:


form1


Now, the form should behave like that:

  • when new action is hit, it should show all categories - unchecked,
  • when create action is hit, but validations are not passing it should render the new view, but keeping the checkboxes that were selected,
  • when edit action is hit, is should show all categories and current categories of the edited organisation should be checked,
  • when create and update are successful, flow is redirected to show action.

We don't want to spoil your minds with our custom solution, but to get to know if there is a clear, Rails solution for that.

In our opinion, there is no such solution in Rails documentation, as we found out reading following sources:

API documentation doesn't get to many-to-many relation, which is the case in our app.


In advance, thanks for help!

Stewart Mckinney

unread,
Apr 19, 2016, 11:17:16 AM4/19/16
to rubyonra...@googlegroups.com

--
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 post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f61e6b1f-958f-4018-b73f-b5495ebef2a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Frederick Cheung

unread,
Apr 19, 2016, 11:19:21 AM4/19/16
to Ruby on Rails: Talk


On Tuesday, April 19, 2016 at 4:09:21 PM UTC+1, Piotr Brych wrote:

We are working with nested attributes and we've reached a point where we're not sure if there is a better, clearer, "more rails" way of dealing with it.

We have 3 models - Organisation, Category and a strictly join table CategoryOrganisation (no additional attributes). The relation between Organisation and Category is many-to-many.

Now, we want to create a form with checkboxes through which the categories for organisations can be added and removed. The example form would look like that:


As long as you don't want the same form to be usable to create new categories, I wouldn't use nested attributes at all.

f.collection_check_boxes :category_ids, Category.all, :id, :name

will render a checkbox and label for each category, and the check box will be ticked if category_ids contains the element (there are plenty of options to customise the generated markup). I'm not entirely sure what happens if you assign to foo_ids but the overall save fails and rolls back. If you run into issues there  I'd add a virtual attribute to hold the assigned ids and actually set category_ids  from a before_save or similar.

Fred                                    

Piotr Brych

unread,
Apr 20, 2016, 9:26:20 AM4/20/16
to Ruby on Rails: Talk
OK, thanks for that, it might help.

But how would you in this case send params back to `create` or `update` to respectively know which associations (category_organisations) should be created and which deleted?

Thanks,
Piotr

Frederick Cheung

unread,
Apr 20, 2016, 1:54:07 PM4/20/16
to Ruby on Rails: Talk
On Wednesday, April 20, 2016 at 2:26:20 PM UTC+1, Piotr Brych wrote:
> OK, thanks for that, it might help.
>
>
> But how would you in this case send params back to `create` or `update` to respectively know which associations (category_organisations) should be created and which deleted?

Assigning to category_ids will create/delete rows in the join table as appropriate.

Fred.

Stewart Mckinney

unread,
Apr 20, 2016, 2:04:59 PM4/20/16
to rubyonra...@googlegroups.com
Piotr, have a look at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

There are a ton of things that are auto-created by associations - one is a setter method that reflects the id's of the association. #collection_check_boxes basically formats your input to match that setter. In this case it would be "category_ids".

--
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 post to this group, send email to rubyonra...@googlegroups.com.

Piotr Brych

unread,
May 4, 2016, 8:14:49 AM5/4/16
to Ruby on Rails: Talk
Thank you guys, you helped us a lot! Great to have you around :).

Piotr
Reply all
Reply to author
Forward
0 new messages