以下の記事は、調査の結果現状のRuby on Rails では期待通りに動かないことがわかりました。
Rails-2.1の新機能であるnamed_scop
1 def gadgets_on_sidebar(place = 'index') 2 case place.to_s 3 when 'index'; Gadget.on_index 4 when 'show'; Gadget.on_show 5 else Gadget.all 6 end.with_scope {active_gad gets + Gadget.mandatorie s} 7 end
各スコープメソッドは、ActiveReco
オブジェクトを返すので、
これを条件にしたがって選択し、最後に.with_scop
を呼んでスコープを適用します。
そこで、
with_named
1 def gadgets_on_sidebar(place = 'index') 2 case place.to_s 3 when 'index'; Gadget.on_index 4 when 'show'; Gadget.on_show 5 else Gadget.all 6 end.with{active_gad gets + Gadget.mandatorie s} 7 end
ということで、Rails本家にもpatchを送っておきました。
posted by
genki
on Fri 1 Aug 2008
at 13:52
with
3 comments
どういうわけか
.all
を呼ぶと誤記していたので.with_scope
に訂正しました。と思ったけれど、どうやら
AR.some_scope.with_scope{}
が期待通りに動いてくれない(Rails-2.1)らしい。目下調査中です。
with_scopeと一緒に使う機能がなさそうだったので、Railsにパッチを送りました。
Improved named_scope to be used like as with_scope