2013-02-12 83 views
2

我有三種模式:用戶,指南和favourite_guideline(目的是讓用戶可以創建自己喜歡的指南列表)。在 模型收藏:has_many:通過關聯錯誤'找不到源協會'

我越來越想添加收藏夾

的ActiveRecord :: HasManyThroughSourceAssociationNotFoundError在 GuidelinesController#喜愛 找不到源協會(S)當一個關聯錯誤:最喜歡的還是FavouriteGuideline。試試'has_many:favorites,:through => :favourite_guidelines,:source =>'。它是以下之一:指南還是 :用戶?在指導方針控制器

類用戶<的ActiveRecord :: Base的

has_many :guidelines 
    has_many :favourite_guidelines 
    has_many :favourites, through: :favourite_guidelines 

end 

class Guideline < ActiveRecord::Base 

    belongs_to :user 
    has_many :favourite_recipes 
    has_many :favourited_by, through: :favourite_recipes, source: :user 

end 

class FavouriteGuideline < ActiveRecord::Base 

    belongs_to :guideline 
    belongs_to :user 

end 

我的收藏操作是:

def favourite 
    type = params[:type] 
    if type == "favourite" 
     current_user.favourites << @guideline 
     redirect_to :back, notice: 'You favourited #{@guideline.name}' 

    elsif type == "unfavourite" 
     current_user.favourites.delete(@guideline) 
     redirect_to :back, notice: 'Unfavourited #{@guideline.name}' 

    else 
     # Type missing, nothing happens 
     redirect_to :back, notice: 'Nothing happened.' 
    end 
    end 

回答

2

好吧,

嘗試「的has_many:我的最愛, :through =>:favourite_guidelines,:source =>'。它是:指南還是:用戶?

它是:準則。

has_many :favourites, through: :favourite_guidelines, source: :guideline 

:源 指定由的has_many使用的源關聯名:通過查詢。只有在名稱不能從關聯中推斷時才使用它。 has_many:訂閱者,:通過=>:訂閱將在訂閱中查找訂閱者或訂閱者,除非給出源:。

documentation :)

+0

感謝 - 這仍是給予同樣的錯誤消息。我注意到錯誤標題是'GuidelinesReceiver :: HasManyThroughSourceAssociationNotFoundError在GuidelinesController#最喜歡' - 我已經將最喜歡的動作複製到我的問題上面,以防萬一它是一個問題... – tessad 2013-02-13 06:39:20