鉴于以下关联,我需要参考Question
that a Choice
是通过从Choice
模型。我一直在尝试使用belongs_to :question, through: :answer
来执行此操作。
class User
has_many :questions
has_many :choices
end
class Question
belongs_to :user
has_many :answers
has_one :choice, :through => :answer
end
class Answer
belongs_to :question
end
class Choice
belongs_to :user
belongs_to :answer
belongs_to :question, :through => :answer
validates_uniqueness_of :answer_id, :scope => [ :question_id, :user_id ]
end
我正进入(状态
NameError 未初始化常量User::Choice
当我尝试做的时候current_user.choices
如果我不包括的话,它工作得很好
belongs_to :question, :through => :answer
但我想用它,因为我希望能够做到validates_uniqueness_of
我可能忽略了一些简单的事情。任何帮助,将不胜感激。
您还可以委托:
class Company < ActiveRecord::Base
has_many :employees
has_many :dogs, :through => :employees
end
class Employee < ActiveRescord::Base
belongs_to :company
has_many :dogs
end
class Dog < ActiveRecord::Base
belongs_to :employee
delegate :company, :to => :employee, :allow_nil => true
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)