This document discusses various Rails model design patterns and ActiveRecord associations including has_one, has_many, belongs_to, has_many :through, has_and_belongs_to_many, polymorphic associations, single table inheritance, and self-joins. It provides code examples for implementing has_many :through, has_and_belongs_to_many, polymorphic associations, and single table inheritance. The document also compares when to use has_many :through vs has_and_belongs_to_many and polymorphic associations vs single table inheritance.
6. 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
7. 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 | |
+------------+----------+------+-----+---------+----------------+
8. 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
17. ポリモーフィック実装例
./bin/rails g model Camera iso
./bin/rails g model Pc cpu
./bin/rails g model Review reviewable:references{polymorphic}:index message
18. 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 |
19. class Camera < ActiveRecord::Base
has_many :reviews, as: :reviewable
end
class Review < ActiveRecord::Base
belongs_to :reviewable, polymorphic: true
end
21. 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
22. 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 | |
+------------+--------------+------+-----+---------+----------------+
23. class Player < ActiveRecord::Base
end
class Footballer < Player
end