我在PostgreSQL 9.5中使用了Rails 5.1,并且是Arel表的新手。我正在尝试创建一个查询(我希望该查询最终与其他作用域查询链接),它返回所有相关记录与给定输入匹配的记录。
给定一个weekday_ids: [1, 2, 5, 6]数组,只返回具有active time_slots匹配的的给定weekday_ids。
模型
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 },
]
}
]编辑--在运行过程中,我遵循了Joe关于关系司的文章中的示例,并创建了这个原始的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};
")发布于 2018-04-27 01:19:24
我还在测试这个,但它看起来很有效:
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)
)https://stackoverflow.com/questions/50050341
复制相似问题