-
2009/10/16 16:30
[Rails 2.3.2] acts_as_state_machine から AASMへ変更
こんちには、おがしょーです。
今回は自分の備忘録のためのエントリです。ユーザー認証などでRestfulAuthenticationを使う場合、ステートマシンとして2種類のモジュールが使える。
1. acts_as_state_machine
2. AASM (rubyist-aasm)※今回の流れ。
ググって出てくる参考サイトではほとんどacts_as_state_machineを使用していたので、迷い無くこれをチョイス。
↓
プラグインはGitHubからのインストールでまとめたいので探してみる、が見つからない。
↓
実は長い間アップデートされてない。
↓
後継でAASMなるものを発見。
↓
rubyist-aasmをインストールした後、RestfulAuthenticationをAASMに切り替える。
↓
find_in_stateがエラーになる。
↓
そういえばacts_as_state_machineに複数状態を指定できるパッチ当ててたっけ。。引用元: kaeruspoon様
module ScottBarron
module Acts
module StateMachine
module ClassMethods
protected
def with_state_scope(target_states)
target_states = [target_states] unless target_states.is_a?(Array)
raise InvalidState unless target_states.all? {|s| states.include?(s)}
cond = []
cond_param = []
target_states.each do |st|
cond << "#{table_name}.#{state_column} = ?"
cond_param < {:conditions => [cond.join(" OR "), cond_param].flatten} do
yield if block_given?
end
end
end
end
end
end↓
AASM用に変更してみる・・・が、動かない。AASMがgemだからか??
↓
メソッド名を変えてリトライ。
[ RAILS/config/initializers/patch_aasm.rb ]
module AASM
module Persistence
module ActiveRecordPersistence
module ClassMethods
def find_in_states(number, state, *args)
with_states_scope state do
find(number, *args)
end
end
def count_in_states(state, *args)
with_states_scope state do
count(*args)
end
end
def [...]
-
2009/10/02 16:08
[Rails 2.3.2] モデルのカラムにサブクエリの値が使いたいのですよ
こんにちは、最近めっきり(なにが?)なおがしょ〜です。
ここ数ヶ月はずっとRubyOnRailsを勉強してますが、ようやく数%くらいわかってきた気がします。さて、本題。ウェブアプリケーションで親子関係のあるデータを一覧表示しようとする場合で、子供側のデータを COUNT(*) してみたりしたくなる時があると思います(あるよね?ね??)。深く考えず、はじめにRailsでやってみたのはこんな感じでした↓
# モデル:item.rb
class Item < ActiveRecord::Base
# col: id
# col: name
has_many :sub_item
end
# モデル:sub_item.rb
class SubItem < ActiveRecord::Base
# col: id
# col: item_id
# col: name
belongs_to :item
end
# アクション:items_controller.rb
def index
@items = Item.find :all
end
# ビュー:items/index.html.erb
<% @items.each do |item| %>
<%=h item.sub_items.count %><!— 小モデルの合計を表示する —>
<% end %>上記コードで問題なく実行できましたが、発行されるSQL文は
親モデルのSELECT文+(小モデルのSELECT文×行数)
となり、非常に無駄な気がします。
サブクエリを使えばSQL文は1つで済みますよね。プラグインとか他のやり方とか探したんですが、findbysql()をする以外の方法が見つからなかったので、簡単にサブクエリを使ったカラムがモデルに追加できるプラグインを作ってみました。http://github.com/ogasyo/subquerycolumn
以下のコマンドでさくっとインストールして、 $ script/plungin install git://github.com/ogasyo/subquerycolumn.git親モデルとコントローラーのfind()部分を以下の用に修正するだけ。
# モデル:item.rb
class Item < [...]