-1
我有兩個模型Hotel
和Theme
,我已經應用了has_and_belongs_to_many
關聯關係。如何從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關聯。預先感謝您的幫助。
感謝您的幫助。 –
Vijay - 如果答案解決了您的問題,請將答案標記爲已接受 – Polsonby