我有两个模型Hotel
,Theme
并且我已经has_and_belongs_to_many
对其应用了关联。
我的迁移是
class CreateHotelsThemesTable < ActiveRecord::Migration
def up
create_table :hotels_themes, :id => false do |t|
t.references :hotel
t.references :theme
end
add_index :hotels_themes, [:hotel_id, :theme_id]
add_index :hotels_themes, :hotel_id
end
def down
drop_table :hotels_theme
end
end
型号是:
class Theme < ActiveRecord::Base
attr_accessible :theme_names
has_and_belongs_to_many :hotels
end
class Hotel < ActiveRecord::Base
attr_accessible :hotel_name, :stars, :location
validates :hotel_name, :stars, :location, :presence => true
has_and_belongs_to_many :thumes
end
其中一些记录是:
theme
=> #<Theme id: 5, theme_names: "Friends", created_at: "2013-08-24 20:13:00", updated_at: "2013-08-24 20:13:00">
hotel
=> #<Hotel id: 2, hotel_name: "vijay shree1", stars: "2", location: "South Goa", created_at: "2013-08-24 15:59:53", updated_at: "2013-08-24 15:59:53">
当我跑
>> theme.hotels
=> [#<Hotel id: 2, hotel_name: "vijay shree1", stars: "2", location: "South Goa", created_at: "2013-08-24 15:59:53", updated_at: "2013-08-24 15:59:53">]
工作,但是当我跑步时
>> hotel.themes
!! #<NoMethodError: undefined method `themes' for #<Hotel:0x007f6cd007c080>>
我不明白为什么会发生这种情况,我使用了 has_and_belongs_to_many 关联。提前感谢您对我的帮助。