Ruby on rails Rails 4:访问关联子模型
例如:Ruby on rails Rails 4:访问关联子模型,ruby-on-rails,model,Ruby On Rails,Model,例如: class Category end class Article belongs_to :category end class Comment belongs_to :article end class Votes belongs_to :comment end 如何通过关联访问类别的投票? 同:类别。首先。投票首先,你需要完成你的关联:每个所属的都应该有一个对应的有多个/有一个 class Category has_many :article
class Category
end
class Article
belongs_to :category
end
class Comment
belongs_to :article
end
class Votes
belongs_to :comment
end
如何通过关联访问类别的投票?
同:
类别。首先。投票
首先,你需要完成你的关联:每个所属的都应该有一个对应的有多个/有一个
class Category
has_many :articles
end
class Article
has_many :comments
belongs_to :category
end
class Comment
has_many :votes, class_name: :votes #rails will look for class Vote, so you have to specify that the class name is actually 'votes'
belongs_to :article
end
然后用适当的,有很多通过联想
class Category
has_many :articles
has_many :comments, through: :articles
has_many :votes, through: :comments
end
现在您应该能够调用Category.first.voces
并获得第一个类别的所有投票