-1

我有兩個模型HotelTheme,我已經應用了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關聯。預先感謝您的幫助。

回答

4

看來有一個錯字:has_and_belongs_to_many :thumes

我認爲這是您問題的原因

+0

感謝您的幫助。 –

+0

Vijay - 如果答案解決了您的問題,請將答案標記爲已接受 – Polsonby