1

我有兩個模型,Like和Photo。Rails 3 - 模型問題,外鍵

class Like < ActiveRecord::Base 
    belongs_to :photo, :class_name => "DataLike", :foreign_key => "photo_id" 
end 

class Photo < ActiveRecord::Base 
    has_many :likes 
end 

現在我試圖執行此查詢:

query = Like.select(:photo_id).joins(:photo).count 

但我仍然得到這個錯誤:

uninitialized constant Like::DataLike

誰能幫助我,請我在做什麼錯?

謝謝你這麼多

回答

3

你似乎並不有DataLike模型,我最好的猜測是,你要鏈接到Photo型號:

class Like < ActiveRecord::Base 
    belongs_to :photo, :foreign_key => "photo_id" 
end 

class Photo < ActiveRecord::Base 
    has_many :likes 
end 

如果你離開了:class_name選項,推斷出Photo模型。它用於指定鏈接模型的類,以防與關聯名稱不同。