I need a lot for many AND habtm-checkboxes!

For some reason, nothing is stored in my groups_user table below, this is information.

My models

group.rb

class Group < ActiveRecord::Base has_and_belongs_to_many :users end 

user.rb

 class User < ActiveRecord::Base has_and_belongs_to_many :groups end 

groups_user.rb

 class GroupsUser < ActiveRecord::Base end 

Entity Relationship Diagram

http://i.imgur.com/MqWok.png

The form

http://i.imgur.com/BwwaV.png

Submission code

 <% for group in @groups %> <%= check_box_tag "user[group_ids][]", group.id, @user.groups.include?(group) %> <%= group.description %> <% end %> 

users_controller.rb

 def update @user = User.find(params[:id]) params[:user][:group_ids] ||= [] if @user.update_attributes(params[:user]) flash[:success] = "User updated." redirect_to @user #end else @title = "Edit user" render 'edit' end end 
+4
source share
2 answers

it should be groups_users not groups_user and you don't need to create a model for this table

+2
source

I had the same problem and my head went astray:

The solution for me simply added this to my user model (app / models / user.rb)

 class User < ActiveRecord::Base attr_accessible :group_ids [...] end 

Hope this helps!

+3
source

All Articles