Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Arel表关系划分_Ruby On Rails_Ruby On Rails 5_Arel_Relational Division - Fatal编程技术网

Ruby on rails Arel表关系划分

Ruby on rails Arel表关系划分,ruby-on-rails,ruby-on-rails-5,arel,relational-division,Ruby On Rails,Ruby On Rails 5,Arel,Relational Division,我将Rails 5.1与PostgreSQL 9.5结合使用,我对Arel表是新手。我正在尝试创建一个查询(我希望最终与其他scopes查询链接),该查询返回所有相关记录与给定输入匹配的记录 给定一个工作日ID数组:[1,2,5,6],只返回具有活动时隙的范围,该范围匹配所有工作日ID 型号 class Range < ApplicationRecord has_many :time_slots end class TimeSlot < ApplicationRecord

我将Rails 5.1与PostgreSQL 9.5结合使用,我对Arel表是新手。我正在尝试创建一个查询(我希望最终与其他scopes查询链接),该查询返回所有相关记录与给定输入匹配的记录

给定一个
工作日ID数组:[1,2,5,6]
,只返回具有活动
时隙的
范围
,该范围匹配所有
工作日ID

型号

class Range < ApplicationRecord
  has_many :time_slots
end

class TimeSlot < ApplicationRecord
  belongs_to :range
  belongs_to :weekday
end
def self.qualified_time_slots(weekday_ids = nil)
  weekday_ids ||= [1, 2, 5, 6]

  qualified_ranges = Range.includes(:time_slots).all.map do |range|
    active_time_slots = range.time_slots.where(active: true)
    range if weekday_ids.all? { |day| active_time_slots.map(&:weekday_id).include? day }
  end

  # return
  qualified_ranges.compact
end
当前在Arel查询中的非工作尝试,以实现与上述方法相同的效果

Range.joins(
  :time_slots
).where(
  time_slots: { active: true }
).where(
  TimeSlot.arel_table[:weekday_id].in(weekday_ids)
)
预期结果

# Should return:
[
  range: {
    id: 1, 
    time_slots: [
      { weekday_id: 1 },
      { weekday_id: 2 },
      { weekday_id: 5 },
      { weekday_id: 6 },
    ]
  },
  range: {
    id: 2, 
    time_slots: [
      { weekday_id: 0 },
      { weekday_id: 1 },
      { weekday_id: 2 },
      { weekday_id: 3 },
      { weekday_id: 4 },
      { weekday_id: 5 },
      { weekday_id: 6 },
    ]
  }
]


# Should NOT return 
[
  range: {
    id: 3, 
    time_slots: [
      { weekday_id: 1 },
      { weekday_id: 2 },
      { weekday_id: 5 },
    ]
  },

  range: {
    id: 4, 
    time_slots: [
      { weekday_id: 0 },
      { weekday_id: 6 },
    ]
  }
]
编辑-在进行这项工作时,我遵循了a中的示例,创建了这个原始SQL查询,它似乎可以工作,但尚未经过彻底测试:

ActiveRecord::Base.connection.exec_query("
  SELECT 
    ts.range_id
  FROM 
    time_slots AS ts
  WHERE 
    ts.active = true
  AND 
    ts.weekday_id IN (#{weekday_ids.join(',')})
  GROUP BY 
    ts.range_id 
  HAVING COUNT(weekday_id) >= #{weekday_ids.length};
")

我仍在测试此功能,但它似乎正在工作:

Range.joins(
  :time_slots
).where(
  TimeSlot.arel_table[:active].eq(
    true
  ).and(
    TimeSlot.arel_table[:weekday_id].in(
      weekday_ids
    )
  )
).group(
  Range.arel_table[:id]
).having(
  TimeSlot.arel_table[:weekday_id].count(true).gteq(weekday_ids.count)
)

s中链接
是否有效?我怀疑链接它们会导致出现and,不确定,只是一个猜测,没有足够的时间来测试和正确回答。无论如何,您需要and,而不是OR