Forum: Ruby on Rails HABTM Checkboxes - Adding/Updating Users to a Project

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
26fee0de276c117f42f123ea8583d93a?d=identicon&s=25 newbie (Guest)
on 2006-12-27 02:09
(Received via mailing list)
Hello all,
I'm trying to create a HABTM checkbox action that adds/updates 'users'
to a 'project'.
I have the following laid out.

  create_table "assignments", :id => false, :force => true do |t|
    t.column "project_id", :integer, :default => 0, :null => false
    t.column "user_id", :integer, :default => 0, :null => false

  create_table "projects", :force => true do |t|
    t.column "title", :string
    t.column "description", :text
    t.column "start_date", :datetime
    t.column "end_date", :datetime

  create_table "users", :force => true do |t|
    t.column "first_name", :string
    t.column "last_name", :string

# Assignment
class Assignment < ActiveRecord::Base
belongs_to    :project
belongs_to    :user

class Project < ActiveRecord::Base
has_many  :assignments
has_many  :users, :through => :assignments

# User
class User < ActiveRecord::Base
  has_many  :assignments
  has_many  :projects, :through => :assignments

So at this point I created dummy data for my "assignment" table in the

# Assignment data
project_id              user_id
1                       1
1                       2
1                       3
2                       1
3                       1

So as you can see project_id 1 contains 3 unique users to the project
and they are user_id 1, 2, 3. Which I think is how i want the model to
work. I think!?

So when I go to my projects controller I have the standard scaffold
generated code for update.

  def update
    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
      flash[:notice] = 'Project was successfully updated.'
      redirect_to :action => 'show', :id => @project
      render :action => 'edit'

when I go to the projects view/_form.rhtml I have this code to pull in
the Users associated with a particular project.

# projects/_form.html
<% User.find(:all, :order => "last_name ASC").each do |u|  %>
 <%= check_box_tag 'project[user_id][]',,
@project.users.include?(u) %> <%= u.first_name %> <%= u.last_name
<% end %>

This works well because it will pull in all users from the users table
and it will check off the checkbox for all the ones that are associated

with the project.

The issue I'm running into is how to "add/update/remove" users from a
project. I think it might be my check box tag.

# projects/_form.html
 <%= check_box_tag 'project[user_id][]',,
@project.users.include?(u) %> <%= u.first_name %> <%= u.last_name

Does this make sense...
# project[user_id][]
<%= check_box_tag project[user_id][]'....

When I try to make any updates to a projects assigned user list (in
this example I'm removing user_id 3 from the project) I get the
following error

NoMethodError in ProjectsController#update

undefined method 'user_id=' for #<Project:0x224b564>

"project"=>{"end_date(3i)"=>"25", "start_date(1i)"=>"2006",
"title"=>"Test Project",
"start_date(2i)"=>"12", "client_id"=>"3", "start_date(3i)"=>"23",
"description"=>"Lore ver sisit nit ut loreet .",

Final thought / question
What's interesting is the 'updated' values are in the parameters
("user_id"=>["1","2"]) but they are not updating the join table of

I think I might be relying on ActiveRecords Magic too much in hoping it

would update the 'assignments' table. Does anyone know what I can do to

remedy the problem.

Should I rethink my set-up or is it the check_box_tags value
"project[user_id][]" that is the problem?

Any thoughts would be helpful.
This topic is locked and can not be replied to.