I have three models: User, FavCategory, and Category. Obviously the FavCategory DB table is nothing more than an association table.
I currently do not have this setup as a HABTM association, because I'm only needing to reference the favorite categories with the current user.
# User.rb
has_many :fav_categories
# FavCategory.rb
belongs_to :user
belongs_to :category
Now I'm wanting to allow the user to choose from checkboxes and select this categories they want as their favorites, I currently have this:
<div class="field">
<%= f.label 'Fav Categories' %>
<ul>
<% Category.all.each do |c| %>
<li>
<%= check_box_tag 'user[fav_category_ids][]', c.id, @user.fav_categories.pluck(:category_id).include?(c.id) %>
<%= c.title %>
</li>
<% end %>
</ul>
<% end %>
</div>
So my questions are:
- Should I use HABTM? Or is it not necessary?
- If HABTM isn't necessary, how would I name the checkboxes in order for them to update the user's fav categories upon saving?
Thanks!