はじめに
Railsのプロジェクトで初期データを作成する方法の1つにseed-fuがあります。
本エントリーではseed-fuを使って関連付け (アソシエーション)のある初期データの作成方法をご紹介します。
seed-fuについて
seed-fuはRailsで初期データを用意するgemの1つです。
詳細は、以下のエントリーをご覧下さい。
railsで初期データを入れる(seed-fuの使い方) - Qiita
関連付け (アソシエーション)のある初期データを入れる
この例ではAuthorモデルとBookモデルの多対多の初期データを用意します。
db/fixtures/以下のseedファイルはアルファベッド順に読み込まれるため、ファイル名のprefixに数字を入れることで、読み込む順番を制御します。
$ tree db/fixtures/
db/fixtures/
├── 01_book.rb
├── 02_author.rb
└── 03_book_author.rb
db/fixtures/01_book.rb
Book.seed do |s|
s.id = 1
s.name = "Book 1"
s.published_on = Time.zone.now
s.price = 1_500
s.number_of_page = 150
end
Book.seed do |s|
s.id = 2
s.name = "Book 2"
s.published_on = Time.zone.now
s.price = 1_500
s.number_of_page = 200
end
db/fixtures/02_author.rb
Author.seed do |s|
s.id = 1
s.name = "Hiromitsu Ito"
s.penname = "Umeyuki Anayama"
end
db/fixtures/03_book_author.rb
BookAuthor.seed do |s|
s.id = 1
s.book = Book.find_by(name: "Book 1")
s.author = Author.find_by(name: "Hiromitsu Ito")
end
BookAuthor.seed do |s|
s.id = 2
s.book = Book.find_by(name: "Book 2")
s.author = Author.find_by(name: "Hiromitsu Ito")
end
$ bin/rake db:seed_fu
== Seed from /Users/umeyuki/dev/src/github.com/umeyuki/seed_fu_with_association/db/fixtures/01_book.rb
- Book {:id=>1, :name=>"Book 1", :published_on=>Fri, 02 Sep 2016 07:57:55 UTC +00:00, :price=>1500, :number_of_page=>150}
- Book {:id=>2, :name=>"Book 2", :published_on=>Fri, 02 Sep 2016 07:57:55 UTC +00:00, :price=>1500, :number_of_page=>200}
== Seed from /Users/umeyuki/dev/src/github.com/umeyuki/seed_fu_with_association/db/fixtures/02_author.rb
- Author {:id=>1, :name=>"Hiromitsu Ito", :penname=>"Umeyuki Anayama"}
== Seed from /Users/umeyuki/dev/src/github.com/umeyuki/seed_fu_with_association/db/fixtures/03_book_author.rb
- BookAuthor {:id=>1, :book=>#<Book id: 1, name: "Book 1", published_on: "2016-09-02", price: 1500, number_of_page: 150, created_at: "2016-09-02 07:52:54", updated_at: "2016-09-02 07:52:54">, :author=>#<Author id: 1, name: "Hiromitsu Ito", penname: "Umeyuki Anayama", created_at: "2016-09-02 07:52:54", updated_at: "2016-09-02 07:52:54">}
- BookAuthor {:id=>2, :book=>#<Book id: 2, name: "Book 2", published_on: "2016-09-02", price: 1500, number_of_page: 200, created_at: "2016-09-02 07:52:54", updated_at: "2016-09-02 07:52:54">, :author=>#<Author id: 1, name: "Hiromitsu Ito", penname: "Umeyuki Anayama", created_at: "2016-09-02 07:52:54", updated_at: "2016-09-02 07:52:54">}
== Seed from
結果
irb(main):002:0> Author.find_by(name: "Hiromitsu Ito").books
Author Load (0.6ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? LIMIT 1 [["id", 1]]
Book Load (1.2ms) SELECT "books".* FROM "books" INNER JOIN "book_authors" ON "books"."id" = "book_authors"."book_id" WHERE "book_authors"."author_id" = ? [["author_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Book id: 1, name: "Book 1", published_on: "2015-06-15", price: 1500, number_of_page: 150, created_at: "2015-06-15 10:42:55", updated_at: "2015-06-15 10:42:55">, #<Book id: 2, name: "Book 2", published_on: "2015-06-15", price: 1500, number_of_page: 200, created_at: "2015-06-15 10:42:55", updated_at: "2015-06-15 10:42:55">]>
サンプルは以下のgithubリポジトリにあります。
umeyuki/seed_fu_with_association