2
使用Rails 3.2/Ruby 1.9的,比如我有以下類:ActiveRecord的關係與範圍
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
現在,如果我有美孚(F)的實例,而我呢f.bars
,我得到一個實例的ActiveRecord ::關係如預期。由於某種原因,雖然當我做f.bars.active
時,我得到了未定義的方法active
關係對象(我會購買這個作爲範圍在Bar
類)。雖然我可以做f.bars.where(:active=>true)
沒問題。我想我的問題是:
- 這裏發生了什麼?
- 如何使用活動範圍來達到所需的結果?
上面的例子做的工作。我的情況是我的班級有一個錯誤(默默失敗),只有當我做Bar.active時才發現。 – psulightning 2014-10-08 15:22:25