微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – Rails has_many通过查询,具体取决于through table属性

有一些has_many通过查询的问题.

使用此处的示例:http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

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

Appointment表有一个名为appointment_date的列

如何让特定医师的所有患者在指定日期预约?

解决方法

Patient.includes(:physicians,:appointments).where('physicians.id = ? AND appointments.appointment_date = ?',<id or ids array>,Date.today)

Date.today可以用任何东西改变,而pysician由id或一组id指定.

你也可以这样做:

physician = Physician.find(id)
patients = physician.patients.includes(:appointments).where('appointments.appointment_date  = ?',some_date)

编辑:

在Rails 4和转发中,您需要向查询添加引用(:约会)以便在where子句中使用约会.

原文地址:https://www.jb51.cc/ruby/269302.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐