Playground
って作れないのかなと思ってググったら、gistが2件引っかかった。
これを元に自分なりのActiveRecord用のPlaygroundを作った。
futoase-sandbox/ActiveRecordPlayground
使う前に
> bundle ins --path
で、必要なgemをインストールする
使い方
pryで作成したmodelのテスト
> bundle ex rake pry
で、試すことができる。
[1] (^_-)-☆ > (main)> Seed.build
[2] (^_-)-☆ > (main)> f = Fruit.new
=> #<Fruit:0x007fbcca58eec0 id: nil, item_type_id: nil, name: nil, price: nil>
[3] (^_-)-☆ > (main)> taxes = f.taxes.new
=> #<Tax:0x007fbcc8d0ef80 id: nil, tax_typable_id: nil, tax_typable_type: "Fruit">
[4] (^_-)-☆ > (main)> tax_types = taxes.tax_types.new
=> #<TaxType:0x007fbcc8a66350 id: nil, tax_id: nil, name: "eight">
[5] (^_-)-☆ > (main)> tax_type = taxes.tax_types.new
=> #<TaxType:0x007fbcca055ee0 id: nil, tax_id: nil, name: "eight">
[6] (^_-)-☆ > (main)> tax_type.name = :eight
=> :eight
[7] (^_-)-☆ > (main)> tax_type.save!
D, [2016-02-04T01:56:33.730505 #13750] DEBUG -- : (0.1ms) begin transaction
D, [2016-02-04T01:56:33.732197 #13750] DEBUG -- : SQL (0.3ms) INSERT INTO "taxes" ("tax_typable_type") VALUES (?) [["tax_typable_type", "Fruit"]]
D, [2016-02-04T01:56:33.733426 #13750] DEBUG -- : SQL (0.1ms) INSERT INTO "tax_types" ("name", "tax_id") VALUES (?, ?) [["name", "eight"], ["tax_id", 1]]
D, [2016-02-04T01:56:33.734465 #13750] DEBUG -- : SQL (0.1ms) INSERT INTO "tax_types" ("name", "tax_id") VALUES (?, ?) [["name", "eight"], ["tax_id", 1]]
D, [2016-02-04T01:56:33.735222 #13750] DEBUG -- : (0.1ms) commit transaction
=> true
テストの実行
minitestを使ってテストを書いている。
> bundle ex rake test
なんで作ったのか
polymorphicな関係のModelを作る時に、
わざわざRails Appでscaffold使ってModel作るのが大げさだと感じたから。
今後しばらくこういうヤツはお世話になる気がする。