0

我正在使用 rails 3.2.3 并且遇到了 HABTM 关系问题。

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

一切正常,但是,我现在想在 User 模型和 Hotel 模型之间添加另一个 HABTM 关系。

除了当我尝试运行我的页面时,我得到一个(tail -f log/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

在这段新的关系之前,一切都在进行中。我根本无法在项目中运行任何页面。有小费吗?

谢谢你们。

4

1 回答 1

2

分裂:

 has_and_belongs_to_many :roles, :hotels

在两行:

 has_and_belongs_to_many :roles
 has_and_belongs_to_many :hotels
于 2012-05-08T18:24:19.793 回答