DataMapper
下記のような感じで記述する。 report_collection = ProductReport.aggregate( # grouping key :product_id, :date, # summary target :amount.sum, :tax.sum, # condition :date => (start_date .. end_date) ) # => [ # [1, '2009-12-14', 100, 10], # [1,…
こんな感じで OK だった。 class TargetModel include DataMapper::Resource property :status, Enum[:born, :live, :dead] end TargetModel.properties[:status].type.flag_map # => {1 => :born, 2 => :live, 3 => :dead} 蛇足ながら、Symbol から数値への…
Model の with_deleted メソッドの block 内で参照を行う。 target_model = TargetModel.with_deleted { TargetModel.get(2) } 複数の Model をまとめて、 with_deleted なモードで扱いたいんだけど、そういうのはないみたい。
一つ一つの model で include とか extend とか書くのはかっこ悪いので、全ての model で自動で取り込むように設定する。
Mix-in を include しても before :save などに、 Hook を仕掛けることができない。 before は module が定義される時に通常の method として実行されるが、そのモジュールには :save というメソッドは存在しないためである。 module が対象の model に incl…
model A と B に association があって、 A は has n, :b B は belongs_to :a という状態だとする。 普通は A#b.id で A の親の B の id が取得できそうなものだけど、これがなぜか取得できない。A#b_id でもダメ。こんな時は、次のように対処する。 A.b # 1…
Transaction を使用中に更に Transaction を使用すると、内側の Transaction で commit が発生したときに、一旦 commit されてしまって都合が悪いので、 commit を最後にまとめて行うライブラリを作成した。↓ライブラリ本体 class SafeTransaction def self.…
Transaction 自体は次のような書き方で動く。http://www.datamapper.org/doku.php?id=docs:transactions には他の記法が紹介されているけど、こっちの方がシンプルで楽。 DataMapper::Transaction.new(User) : (なんか処理) : end Transactionを多重に行うと…
Model にこれを書くだけで、論理削除できるみたい。論理削除は Paranoia なんだ...。 property deleted_at, ParanoidDateTime 論理削除したものを無理矢理にでも読み込むのはどう実現するのだろうか?