如何解决如何在联接模型上编写“作用域”?
| 假设我有这些模型:Physician
通过其Appointments
有许多many1ѭ。
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients,:through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians,:through => :appointments
end
我想编写一个范围或类似的内容,以便可以找到已确认任命的给定医师的所有患者。
最惯用的方法是什么?
解决方法
使用has_many:through关联来做到这一点:
has_many :confirmed_patients,:through => :appointments,:source => :patient,:class_name => \'Patient\',:conditions => { :confirmed => true }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。