Take the 2-minute tour ×

Im using rails 3.2.3 and am having issues with HABTM relationships.

Basically, I have a User model, with has a HABTM with a Role model.

Everything works fine, however, I now want to add another HABTM relation between the User model and the Hotel model.

Except for when I try to run my page I get a (tail -f log/development.log):

NoMethodError (undefined method `[]' for :hotels:Symbol):

My routes:

Oops::Application.routes.draw do #this is my line 1
  get "managers/index"

 root :to => "hotels#index"

  devise_for :users #this is my line 6
  resources :roles
  resources :users
  resources :managers
  resources :hotels


My User model:

devise :database_authenticatable, :recoverable, (...)

has_and_belongs_to_many :roles, :hotels #This is my line 7, if I delete :hotels and all its references in this HABTM relationship, everything works

accepts_nested_attributes_for :roles

attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :role_ids, :hotel_attributes

My Hotel model:

has_and_belongs_to_many :facilities, :users
  has_one :address
  accepts_nested_attributes_for :facilities, :address, :users
  attr_accessible <all hotel attributes>, :facility_ids, :address_attributes, :user_ids

Everything was working up until this new relationship. I can't run any page in the project at all. Any tips?

Thank you all.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted


 has_and_belongs_to_many :roles, :hotels

on two lines:

 has_and_belongs_to_many :roles
 has_and_belongs_to_many :hotels
share|improve this answer
add comment

Your Answer


