以下の記事は、調査の結果現状のRuby on Rails
では期待通りに動かないことがわかりました。


Rails-2.1の新機能であるnamed_scopeを使うと、従来のwith_scope
では綺麗にかけなかったスコープの選択を簡潔に記述することができます。

rails>>
def gadgets_on_sidebar(place = 'index')
case place.to_s
when 'index'; Gadget.on_index
when 'show'; Gadget.on_show
else Gadget.all
end.with_scope{active_gadgets + Gadget.mandatories}
end
<<--

各スコープメソッドは、ActiveRecord::NamedScope::Scope
オブジェクトを返すので、
これを条件にしたがって選択し、最後に.with_scope
を呼んでスコープを適用します。


そこで、
with_named_scope
というGemプラグインをつくり、
上述のように処理できるようにしました。
以下のようになります。

rails>>
def gadgets_on_sidebar(place = 'index')
case place.to_s
when 'index'; Gadget.on_index
when 'show'; Gadget.on_show
else Gadget.all
end.with{active_gadgets + Gadget.mandatories}
end
<<--

ということで、Rails本家にもpatchを送っておきました。

posted by genki genki on Fri 1 Aug 2008 at 16:06 with 3 comments
瀧内元気 瀧内元気 08/01 16:07
どういうわけか`.all`を呼ぶと誤記していたので`.with_scope`に訂正しました。
瀧内元気 瀧内元気 08/01 17:09
と思ったけれど、どうやら`AR.some_scope.with_scope{}` が期待通りに動いてくれない(Rails-2.1)らしい。

目下調査中です。
瀧内元気 瀧内元気 08/01 19:59
with_scopeと一緒に使う機能がなさそうだったので、Railsにパッチを送りました。

[Improved named_scope to be used like as with_scope ](http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/739)
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
http://rlu.ru/5cfBn http://rlu.ru/5cfBn: Greetings auto enthusiasts, gotta share а amazing ... 05/16 18:38
https://xn--9m1bj7w5fr3e8te72a7k351ewff.kr/bbs/board.php?bo_table=free&wr_id=201227 https://xn--9m1bj7w5fr3e8te72a7k351ewff.kr/bbs/board.php?bo_table=free&wr_id=201227: Unlock Singapore's shoppng secrets at Kaizenaire.ⅽ... 05/16 17:50
https://alexander-tech.org/ https://alexander-tech.org/: I need to to thank you for this excellent read!! ... 05/16 11:56
https://unosport.pl/ https://unosport.pl/: Hmm is anyone else experiencing problems with the ... 05/16 10:37
https://strategyreviewer.com/ https://strategyreviewer.com/: Marvelous, what a web site it is! This blog presen... 05/16 10:01
https://listji.com/index.php?page=user&action=pub_profile&id=72479&item_type=active&per_page=16 https://listji.com/index.php?page=user&action=pub_profile&id=72479&item_type=active&per_page=16: As tһe premier furniture store аnd comprehensive f... 05/16 07:16
https://khvanchkaravino.ru https://khvanchkaravino.ru: Квартира мечты за 2 месяца — помогаем выбрать мебе... 05/16 05:06
https://www.tequilagames.com https://www.tequilagames.com: Thanks for finally writing about >something new an... 05/16 03:16