【发布时间】:2016-03-02 09:35:15
【问题描述】:
我已经搜索和搜索,只找到了我当前问题的部分解决方案。
问题是,我想知道是否可以在 Ruby on Rails 中使用 has_many :through 和多态关联。
我有一个系统,students 可以为他们的项目创建travel plans(可以属于许多students)和refund claims(只能属于一个student)。在这个系统中,admin users 和students 都可以对计划和索赔进行评论。
我的联想是:
class Student < ActiveRecord::Base
has_and_belongs_to_many :travel_plans
has_many :refund_claims
has_many :comments, through: :travel_plans
has_many :comments, through: :refund_claims
end
class AdminUser < ActiveRecord::Base
has_many :comments
end
class TravelPlan < ActiveRecord::Base
has_and_belongs_to_many :students
has_many :comments, as: :commentable
end
class RefundClaim < ActiveRecord::Base
belongs_to :student
has_many :comments, as: :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
我的问题是:
在Student模型中关联两次comments是否正确?
我不希望AdminUsers 拥有travel plans 和refund claims,我如何识别他们的comments 是在travel plan 或refund claim 上创建的?
会有更好的方法吗?
提前非常感谢大家!
干杯,
【问题讨论】:
标签: ruby-on-rails activerecord associations