Habtm


#1

Hi.

Im new to Ruby and new to Ruby on Rails and have problems realizing a
simple many_to_many relationship:

My tables:

items <-> items_users <-> users


id user_id id
name item_id name
… password

My Code:

class Item < ActiveRecord::Base
has_and_belongs_to_many :users
end

class User < ActiveRecord::Base
attr_accessor :new_password
has_and_belongs_to_many :items
[…]
end

items/list.rhtml:
[…]

<%= link_to 'Ã', :action => 'select', :id => item %> [....]

class ItemsController < ApplicationController
[…]
def select
@user = @session[‘user’]
item = Item.find(@params[:id])
@user.items = Item.find(@params[:id])
#<<--------this is the erroneous line of code!
redirect_to :action => ‘list’
end
[…]
end

With “select” i want to add the current active user one item, whose
id is delivered as request parameter.
What am I doing wrong?

Thx in advance,

Tobias

NoMethodError in Items#select

undefined method `each’ for #Item:0x266b13c

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace

/dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/
base.rb:1786:in method_missing' /dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/ associations/association_collection.rb:117:inreplace’
/dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/
associations.rb:884:in items=' #{RAILS_ROOT}/app/controllers/items_controller.rb:15:inselect’

/dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/
base.rb:1786:in method_missing' /dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/ associations/association_collection.rb:117:inreplace’
/dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/
associations.rb:884:in items=' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ base.rb:908:inperform_action_without_filters’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
filters.rb:355:in perform_action_without_benchmark' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ benchmarking.rb:69:inperform_action_without_rescue’
/dp/lib/ruby/1.8/benchmark.rb:293:in measure' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ benchmarking.rb:69:inperform_action_without_rescue’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
rescue.rb:82:in perform_action' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ base.rb:379:inprocess_without_filters’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
filters.rb:364:in process_without_session_management_support' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ session_management.rb:117:inprocess’
/dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/dispatcher.rb:38:in
dispatch' /dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:115:inhandle_dispatch’
/dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:81:in
service' /dp/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/dp/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /dp/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/dp/lib/ruby/1.8/webrick/server.rb:162:in start_thread' /dp/lib/ruby/1.8/webrick/server.rb:95:instart’
/dp/lib/ruby/1.8/webrick/server.rb:92:in start' /dp/lib/ruby/1.8/webrick/server.rb:23:instart’
/dp/lib/ruby/1.8/webrick/server.rb:82:in start' /dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:67:indispatch’
/dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/servers/
webrick.rb:59
/dp/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:21:in require' /dp/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/ dependencies.rb:136:inrequire’
/dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/server.rb:30
/dp/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:21:in require' /dp/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/ dependencies.rb:136:inrequire’
script/server:3

/dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/
base.rb:1786:in method_missing' /dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/ associations/association_collection.rb:117:inreplace’
/dp/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/
associations.rb:884:in items=' #{RAILS_ROOT}/app/controllers/items_controller.rb:15:inselect’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
base.rb:908:in perform_action_without_filters' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ filters.rb:355:inperform_action_without_benchmark’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
benchmarking.rb:69:in perform_action_without_rescue' /dp/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
benchmarking.rb:69:in perform_action_without_rescue' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ rescue.rb:82:inperform_action’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
base.rb:379:in process_without_filters' /dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/ filters.rb:364:inprocess_without_session_management_support’
/dp/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/
session_management.rb:117:in process' /dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/dispatcher.rb:38:indispatch’
/dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:115:in
handle_dispatch' /dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:81:inservice’
/dp/lib/ruby/1.8/webrick/httpserver.rb:104:in service' /dp/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
/dp/lib/ruby/1.8/webrick/server.rb:173:in start_thread' /dp/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/dp/lib/ruby/1.8/webrick/server.rb:95:in start' /dp/lib/ruby/1.8/webrick/server.rb:92:instart’
/dp/lib/ruby/1.8/webrick/server.rb:23:in start' /dp/lib/ruby/1.8/webrick/server.rb:82:instart’
/dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:67:in
dispatch' /dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/servers/ webrick.rb:59 /dp/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/dp/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/
dependencies.rb:136:in require' /dp/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/server.rb:30 /dp/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/dp/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/
dependencies.rb:136:in `require’
script/server:3

Request

Parameters: {“id”=>“2”}

Show session dump


user: &id001 !ruby/object:User
attributes:
salt: b40f4edfd113ce8c82e8283281697e8af6d09520
delete_after:
role:
security_token: a61f0622696cdf5d4da640091f73be700ea44f5a
id: “2”
firstname: Tobias
lastname: Bender
token_expiry: 2006-04-05 13:45:59
deleted: “0”
verified: “1”
login: tbender
salted_password: 393e2195368915812e1490528ae3071b2f266e6f
email: removed_email_address@domain.invalid
errors: !ruby/object:ActiveRecord::Errors
base: *id001
errors: {}

items: []

new_password: false
new_record_before_save:
return-to:
flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}


#2

hi there,
try:
@item = Item.find(@params[:id])
@user.items << @item

since @user.items is an array in an n:n relationship you want to push
objects on it rather than make it equal to one object… (rather crudely
put… sorry)

kind regards,
a


#3

Tobias Bender wrote:

class ItemsController < ApplicationController
[…]
def select
@user = @session[‘user’]
item = Item.find(@params[:id])
@user.items = Item.find(@params[:id])
#<<--------this is the erroneous line of code!
redirect_to :action => ‘list’
end
[…]
end

With “select” i want to add the current active user one item, whose
id is delivered as request parameter.
What am I doing wrong?

Thx in advance,

Tobias

Hi,

guess you wanted:
@user.items<< item


Agnieszka