4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveRecordを試すためのPlaygroundを作る

Last updated at Posted at 2016-02-03

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作るのが大げさだと感じたから。

今後しばらくこういうヤツはお世話になる気がする。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?