2012-05-08 140 views
0

Im使用rails 3.2.3,并且遇到与HABTM关系有关的问题。Ruby on Rails - HABTM NoMethodError

基本上,我有一个用户模型,有一个HABTM与角色模型。

一切工作正常,但是,我现在想要在用户模型和酒店模型之间添加另一个HABTM关系。

除了当我尝试运行我的网页,我收到了(尾-f日志/ development.log):

NoMethodError (undefined method `[]' for :hotels:Symbol): 
    app/models/user.rb:7 
    config/routes.rb:6 
    config/routes.rb:1 

我的路线:

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 

    ActiveAdmin.routes(self) 

我的用户模型:

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 

我的酒店模式:

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 

一切都在努力,直到这种新的关系。我无法在项目中运行任何页面。 任何提示?

谢谢大家。

回答

2

斯普利特:

has_and_belongs_to_many :roles 
has_and_belongs_to_many :hotels 
:两行

has_and_belongs_to_many :roles, :hotels