在三个没有活动记录查询中“ where”内部条件的表之间联接

奥斯卡·雷内·巴莱斯特罗斯广场

我有一个查询,我想知道您是否可以帮助我。在我的应用程序中,我有桌子的员工,日历和班次。关系如下:

class Employee < ApplicationRecord
   has_many :calendars
   has_many :shifts, :through => :calendars

class Shift < ApplicationRecord
  has_many :calendars
  has_many :employees, :through => :calendars

class Calendar < ApplicationRecord
  belongs_to :employee
  belongs_to :shift

因此,我需要制作一个显示以下结果的表:

Name                |   Calendars.date  |   Calendars.date  | …
Employee.name       |   Shift.cod       |   Shift.cod       | …
Employee.name       |   Shift.cod       |   Shift.cod       | …
…

为此,我构建了查询:

-@employees = Employee.joins(:calendars, :shifts).where("EXTRACT(MONTH FROM calendars.date) = ?", 12).uniq

结果获得了所有必要的数据,但是我需要显示一个月的数据,但是不会使用我在where参数中输入的内容。

如何建立按月过滤的查询?

狄龙·科尔特斯(Dillon Cortez)

也许你可以尝试 Employee.includes(:calendars, :shifts).where('calandar.month= ?', '<whatever month here>').references(:calendars)

我不确定语法是否正确,但是基本上您可以1st_obj.includes(obj).where(case).references(obj)在属于另一个对象的对象上执行操作

这是active_records方法文档.includes

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章