狠狠撸

狠狠撸Share a Scribd company logo
搁补颈濒蝉モデル设计ケーススタディ
ActiveRecordのアソシエーション
has_one
has_many
belongs_to
has_many :through
has_and_belongs_to_many
ポリモーフィック
STI
自己結合
多対多ケース
記事とタグ
ユーザと興味のあるカテゴリ
どっちつかうの問題1
has_many :through
vs
has_and_belongs_to_many
基本、has_many :through
has_and_belongs_to_manyは、中間テーブルが隠蔽され
ていて一見良さそうに見えるけど、把握しにくい。
has_many :through実装例
./bin/rails g model Product name
./bin/rails g model Order total_price:integer
./bin/rails g model ProductOrder product:references order:references
mysql> desc product_orders;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| product_id | int(11) | YES | MUL | NULL | |
| order_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
class Product < ActiveRecord::Base
has_many :product_orders
has_many :orders, through: :product_orders
end
class ProductOrder < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
Product.first.orders
has_and_belongs_to_manyの実装
例
./bin/rails g model Article title
./bin/rails g model Tag name
./bin/rails g create_join_table_article_tag article tag
mysql> desc articles_tags;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| article_id | int(11) | NO | | NULL | |
| tag_id | int(11) | NO | | NULL | |
+------------+---------+------+-----+---------+-------+
class Article < ActiveRecord::Base
has_and_belongs_to_many :tags
end
Article.first.tags
アソシエーションのオプション
dependent
inverce_of
delegate
touch
どっちつかうの問題2
ポリモーフィック
vs
STI(単一テーブル継承)
目的:共通するインターフェイスを便利に使いたい
決して同じフィールドがあるから共通化しよ~ではない
ポリモーフィック
役割ごとに分割する。操作が違うことがある場合
STI
役割はわりと同じで、一部のデータ構造だけが違う場合
個人的には、MySQLなどを使っている場合はテーブル継
承はRailsの世界で解決しているので、進んで選ぶことは
ないかなーと。本当に必要かどうかは見極め重要。
ポリモーフィック実装例
./bin/rails g model Camera iso
./bin/rails g model Pc cpu
./bin/rails g model Review reviewable:references{polymorphic}:index message
mysql> desc cameras;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| iso | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
mysql> desc reviews;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment
| reviewable_id | int(11) | YES | | NULL |
| reviewable_type | varchar(255) | YES | MUL | NULL |
class Camera < ActiveRecord::Base
has_many :reviews, as: :reviewable
end
class Review < ActiveRecord::Base
belongs_to :reviewable, polymorphic: true
end
c = Camera.create(iso:3200)
c.reviews.create(message: "hoge")
mysql> select * from reviews;
+----+---------------+-----------------+---------+---------------------+-----
| id | reviewable_id | reviewable_type | message | created_at | upda
+----+---------------+-----------------+---------+---------------------+-----
| 1 | 1 | Camera | hoge | 2016-04-05 17:10:48 | 2016
| 2 | 1 | Pc | piyo | 2016-04-05 17:13:03 | 2016
+----+---------------+-----------------+---------+---------------------+-----
STIの実装例
./bin/rails g model Player name goal:integer homerun:integer type
./bin/rails g model footballer --parent player
./bin/rails g model baseballer --parent player
mysql> desc players;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| goal | int(11) | YES | | NULL | |
| homerun | int(11) | YES | | NULL | |
| type | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
class Player < ActiveRecord::Base
end
class Footballer < Player
end
Footballer.create(name: 'hide', goal: 10)
Baseballer.create(name: 'ichiro', homerun: 5)
mysql> select * from players;
+----+--------+------+---------+------------+---------------------+----------
| id | name | goal | homerun | type | created_at | updated_a
+----+--------+------+---------+------------+---------------------+----------
| 1 | hide | 10 | NULL | Footballer | 2016-04-05 17:21:02 | 2016
| 2 | ichiro | NULL | 5 | Baseballer | 2016-04-05 17:21:28 | 2016
+----+--------+------+---------+------------+---------------------+----------
その他時間が余れば
concern
decorator
service
validator
callback
exception
helper

More Related Content

搁补颈濒蝉モデル设计ケーススタディ