2013-02-12 86 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