Peter Piper Peter Piper - 13 days ago 5
Ruby Question

Rails 4 Checkbox helper-table - Many_to_many

I got the following problem: I want to create a site with checkboxes on where a user can select other users for a certain task(like a doodle-clone). I got the view so far that it displays all of the current users available but now i want to let the user choose which one of them is "attending" the task.

I tested the model via console and everything is working so far but i just can't get the the participate boolean in the attendee helper-table set. If i run the code like that i always get a "
NoMethodError in TasksController#participate
undefined method `find_by_user_id' for #" - Error!

Any Ideas?

models:

class Testuser < ActiveRecord::Base
has_many :attendees
has_many :tasks, :through => :attendees
end
class Task < ActiveRecord::Base
has_many :attendees
has_many :testusers, :through => :attendees

class Attendee < ActiveRecord::Base
belongs_to :task
belongs_to :testuser
end


schema.rb:

create_table "attendees", force: true do |t|
t.integer "task_id"
t.integer "testuser_id"
t.boolean "participate"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "tasks", force: true do |t|
t.string "title"
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "task_date"
t.boolean "participate"
end

create_table "testusers", force: true do |t|
t.string "username"
t.datetime "created_at"
t.datetime "updated_at"
end


tasks_controller.rb

def select_user
@testuser = Testuser.all
end

def participate

# "user_checkbox"=>["4","6","8"], => example_output of the parameters
params[:user_checkbox].each do |check|
check_id = check

t = Attendee.find_by_user_id(check_id)
t.update_attribute(:participate, true)

@attendee = Attendee(where participate == true)
end
end


select_user.html.erb

<h1>Select Users</h1>
<%= form_tag("/tasks/participate/", :method => "post") do %>
<ul>
<% @testuser.each do |testuser| %>
<li> <%= check_box_tag "user_checkbox[]",testuser.id %> <%= testuser.username %></li>
<% end %>
</ul>
<%= submit_tag("Teilnehmer hinzuf├╝gen") %>
<% end %>


full stack trace:

activerecord (4.1.6) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
app/controllers/tasks_controller.rb:46:in `block in participate'
app/controllers/tasks_controller.rb:41:in `each'
app/controllers/tasks_controller.rb:41:in `participate'
actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.6) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.6) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/xxx/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/xxx/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/xxx/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

Answer

Your db table attendees doesn't have user_id field:

create_table "attendees", force: true do |t|
  t.integer  "task_id"
  t.integer  "testuser_id"
  t.boolean  "participate"
  t.datetime "created_at"
  t.datetime "updated_at"
end

That's why t = Attendee.find_by_user_id(check_id) doesn't work.

Also your code is very strange and extremely inefficient. You run sql SELECT for each record. You run sql UPDATE for each record without transaction. Also you rewrite @attendee on each step and in the end you have only the last. And this syntax is incorrect: Attendee(where participate == true). You can rewrite everything like this:

# in case if you don't need to run validations and callbacks on each Attendee
# you can make all job by 1 SQL UPDATE
Attendee.where(testuser_id: params[:user_checkbox]).update_all(participate: true)
@attendees = Attendee.where(testuser_id: params[:user_checkbox])

# in case if you need to run validations and callbacks on each Attendee
# you pull all records by 1 query and then update each
@attendees = Attendee.where(testuser_id: params[:user_checkbox])
Attendee.transaction do
  @attendees.each do |attendee|
    attendee.update_attributes!(participate: true)
  end
end
Comments