【发布时间】:2011-01-20 02:58:29
【问题描述】:
我有两个模型,foo 和 bar,foo 有很多 bars。
Bar 是在给定时间段内发生的事件,所以我想要一个返回 ActiveRecord::Relation 的方法或作用域,表示当前具有活动栏的 foo。
这在Foo 类中很简单,有一个作用域:
class Foo < ActiveRecord::Base
has_many :bars
scope :has_current_bars, joins(:bars).where('bar.foo_id IS NOT NULL').where('bar.starts_at <= ?', DateTime.now).where('bar.ends_at >= ?', DateTime.now)
我不喜欢这一点,foo 需要非常了解 bar 的内部结构。
这可以重写吗,可能通过在bar 上添加一个范围,所以foo 不需要知道bar 属性?
【问题讨论】:
标签: ruby-on-rails activerecord named-scope arel