【发布时间】:2014-10-03 15:36:32
【问题描述】:
使用 Rails 3.2/Ruby 1.9,例如我有以下类:
class Foo
has_many :bars
end
class Bar
scope :active, where(:active=>true)
# also tried
# scope :active, lambda{where(:active=>true)}
# scope :active, -> {where(:active=>true)}
end
现在,如果我有一个 Foo (f) 的实例,并且我执行f.bars,我会按预期得到一个 ActiveRecord::Relation 的实例。出于某种原因,尽管当我执行f.bars.active 时,我得到了关系对象的未定义方法active(我会买这个,因为范围在Bar 类上)。我可以做f.bars.where(:active=>true) 没问题。我想我的问题是:
- 这里面到底发生了什么?
- 如何使用主动作用域来达到预期的效果?
【问题讨论】:
-
上面的例子确实有效。我的情况是我的班级有一个错误(默默地失败),只有在我做 Bar.active 时才发现。
标签: ruby-on-rails activerecord scope