【发布时间】:2019-04-04 18:20:58
【问题描述】:
我想根据多态关联的属性编写范围。
我有模型:
class Item < ApplicationRecord
belongs_to :person, polymorphic: true
end
class Teacher < ApplicationRecord
has_many :items, as: :person
end
class Student < ApplicationRecord
has_many :items, as: :person
end
Teacher 和 Student 都具有 name 属性,我想将该属性用于以下范围:
scope :by_name, ->(name) { joins(:person).where(persons: {name: name}) }
既然我不能只提到范围,有什么办法可以很好地确定范围吗?
【问题讨论】:
标签: ruby-on-rails scope polymorphic-associations