我有4个Rails 3个范围,我想简化:
scope :age_0, lambda {
where("available_at IS NULL OR available_at < ?", Date.today + 30.days)
}
scope :age_30, lambda {
where("available_at >= ? AND available_at < ?", Date.today + 30.days, Date.today + 60.days)
}
scope :age_60, lambda {
where("available_at >= ? AND available_at < ?", Date.today + 60.days, Date.today + 90.days)
}
scope :age_90, lambda {
where("available_at >= ?", Date.today + 90.days)
}
我想到了一个类方法:
def self.aging(days)
joins(:profile).where("available_at IS NULL OR available_at < ?", Date.today + 30.days) if days==0
joins(:profile).where("available_at >= ? AND available_at < ?", Date.today + 30.days, Date.today + 60.days) if days==30
joins(:profile).where("available_at >= ? AND available_at < ?", Date.today + 60.days, Date.today + 90.days) if days==60
joins(:profile).where("available_at >= ?", Date.today + 90.days) if days==90
end
但我不知道该返回什么来确保它与Rails3范围兼容。
这是个好方法吗?有更好的方法吗?
**编辑**
我更新了方法的逻辑。中频测试似乎不像我预期的那样有效。