ä»åã¯Railsã®ã¢ã½ã·ã¨ã¼ã·ã§ã³ã«ã¤ãã¦è¨è¼ãã¾ãã
ã¢ã½ã·ã¨ã¼ã·ã§ã³ã¨ã¯ï¼
Railsã§ã¯ãã¢ã½ã·ã¨ã¼ã·ã§ã³ã¨ããæ©è½ãåå¨ããã¢ãã«ã¨ã¢ãã«ãé¢é£ä»ããããã¨ã«ãã£ã¦ä»ã¢ãã«ã®ãã¼ã¿ãåããã¦æä½ãããã¨ãã§ãã¾ãã
ä¾ãã°ãuserã®æ稿ããpostãå ¨ä»¶åå¾ãããå ´åãã¢ã½ã·ã¨ã¼ã·ã§ã³ã®è¨å®ãè¡ã£ã¦ããªãå ´åã«ã¯ãwhereã¡ã½ãããç¨ããå¿ è¦ãããã¾ãã
user = User.find(1) posts = Post.where(user_id: user.id)
ãããã¢ã½ã·ã¨ã¼ã·ã§ã³ã®è¨å®ãè¡ã£ã¦ããå ´åã以ä¸ã®ããã«æ¸ããã¨ãã§ãã¾ãã
user = User.find(1) posts = user.posts
使ç¨ã§ããã¢ã½ã·ã¨ã¼ã·ã§ã³
ããããã¯è¨å®ã§ããã¢ã½ã·ã¨ã¼ã·ã§ã³ã«ã¤ãã¦ç¢ºèªãã¦ããã¾ãã
belongs_to
belongs_to
ã¯ä»ã®ã¢ãã«ã«æå±ãã¦ããå ´åã«ä½¿ç¨ãã¾ãã
ããã¢ãã«ã§belongs_toé¢é£ä»ããè¡ãªãã¨ã宣è¨ãè¡ã£ãã¢ãã«ã®åã¤ã³ã¹ã¿ã³ã¹ã¯ãä»æ¹ã®ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ã«æå±ãã¾ãã
belongs_toã®èª¬æã®ããã«ååã¨ã¬ãã¥ã¼ã®é¢ä¿ã«ã¤ãã¦èª¬æãã¾ãã
ä¾ãã°ãã¤ã¤ãã©ã³ã®åå説æãã¼ã¸ãããã¨ãã¾ããããã«1件ã®ã¬ãã¥ã¼ããã¦ããã¨ãã¦ãbelongs_toã¯ååã«ã¤ããã®ã§ããããï¼ãããã¯ã¬ãã¥ã¼ã«ã¤ããã®ã§ããããï¼
belongs_toã¯ã¬ãã¥ã¼ã«è¨å®ãã¾ããã¬ãã¥ã¼ãååã«å¿
ãå±ãã¦ãã¾ããã¬ãã¥ã¼ã¯ååããªããã°åå¨ããªãã®ã§ãååã«ã¬ãã¥ã¼ãæå±ãã¦ããã¨èãã¾ãã
ã¢ãã«ã¯ä¸è¨ã®ããã«è¡¨ãã¾ãã
class Product < ApplicationRecord end class Review < ApplicationRecord belongs_to :product end
belongs_to :productã®ããã«æå±å ã®ã¢ãã«ãåæ°ã§æå®ãã¾ãã
ã©ã¡ãã«belongs_toãè¨å®ãããè¿·ã£ãæã«ã¯å¤é¨ãã¼ãã©ã¡ãã«ç½®ããèããã¨ããã¨æãã¾ãã
å¤é¨ãã¼ãæã¤æ¹ã«belongs_toãè¨å®ããã®ãæã¾ããã§ãã
å®éã«ãã¼ãã«ãæ¤è¨ããã¨ä¸è¨ã®ããã«ãªãã¨æãã¾ãã
ã»Productãã¼ãã«
id | product_name |
---|---|
1 | ã¤ã¤ãã©ã³A |
2 | ã¤ã¤ãã©ã³A |
ã»Reviewãã¼ãã«
id | product_id | review_point |
---|---|---|
1 | 1 | 100 |
2 | 1 | 70 |
2 | 2 | 30 |
Reviewãã¼ãã«ã«ã¯å¤é¨ãã¼ã®product_idãä½æãã¦ã©ã®ååã®ã¬ãã¥ã¼ããããããã«ãã¦ãã¾ãã ãªã®ã§ãä»åã¯Reviewã«belongs_toãè¨å®ãã¾ãã
has_one
has_one
ã¯ã¢ãã«å士ã1対1ã®é¢ä¿ã®å ´åã«ä½¿ç¨ãã¾ãã
class User < ApplicationRecord has_one :profile end class Profile < ApplicationRecord belongs_to :user end
ä¾ãã°ãä¸è¨ã®ãããªuseråã ãprofileãæã£ã¦ããå ´åã§ãã
å¤é¨ãã¼ãæã¤å´ã«belongs_to
ã使ç¨ããå¤é¨ãã¼ãæããªãå´ãhas_one
ã使ç¨ããã¨ããã®ãåºæ¬çãªä½¿ãåãã«ãªãã¾ãã
ä¸è¨ã®ããã«ãããã¨ã§ãããã£ã¼ã«ã®æ å ±ãåå¾ã§ãã¾ãã
user = User.find(1) // ãããã£ã¼ã«æ å ±ã®åå¾ user.profile
has_many
has_many
ã¯ã¢ãã«å士ã1対å¤ã®é¢ä¿ã®å ´åã«ä½¿ç¨ãã¾ãã
belongs_toã®ã¨ãã«ä½¿ã£ãååã¨ã¬ãã¥ã¼ã®ä¾ããã¨ã«èª¬æãã¾ãã
belongs_toã®èª¬æã®éã«ã¯ååï¼ã¤ã«å¯¾ãã¦ã¬ãã¥ã¼ãï¼ã¤ã¨ããæ¡ä»¶ã§èª¬æãã¾ããã å®éã«ã¯ååï¼ã¤ã«å¯¾ãã¦ãã¬ãã¥ã¼ã¯è¤æ°ã®ãã¿ã¼ã³ã¯ãããã¾ãã
1対å¤ã®é¢ä¿ã®å ´åã«ã¯has_many
ãä¸è¨ã®ããã«è¨å®ãã¾ãã
class Product < ApplicationRecord has_many :reviews end class Review < ApplicationRecord belongs_to :product end
has_manyé¢é£ä»ãã宣è¨ããå ´åãç¸æã®ã¢ãã«åã¯è¤æ°å½¢ã§æå®ããå¿ è¦ãããã®ã§ã注æãå¿ è¦ã§ãã
ååã«ç´ã¥ããã¬ãã¥ã¼ã®æ å ±ã®ä¸è¦§ã¯ä¸è¨æ¹æ³ã§åå¾ã§ãã¾ãã
product = Product.find(1) product.reviews
has_one :through
has_one :through
ã¯1対1ã®ã¤ãªããã®2ã¤ã®ã¢ãã«ã®éã«ç¬¬3ã®ã¢ãã«ãç´ã¥ãã¦ããããããçµç±ãã¦ç¸æã¢ãã«ã®1åã®ã¤ã³ã¹ã¿ã³ã¹ã¨ããããã¾ãã
ä¾ãã°ãuserã¯ä¸ã¤ã®postãæã¡ãpostã¯ä¸ã¤ã®tagãæã¤ã¨ãã¾ãã ãã®å ´å以ä¸ã®ããã«æ§é ã表ç¾ãããã¨ãåºæ¥ã¾ãã
class User < ApplicationRecord has_one :post has_one :tag, through: :post end class Post < ApplicationRecord belongs_to :user has_one :tag end class Tag < ApplicationRecord belongs_to :post end
user has one postã¨post has one tagã¨ããé¢é£æ§ãã¾ãåå¨ãã¾ãã
ããã¦ãUserã¯has_one :tag, through: :post
ã¨ããããã«Postãçµç±ãã¦Tagãï¼ã¤ç´ã¥ãã¦ãããã¨ã表ãã¾ãã
ãããããã¨ã§ãUserããç´æ¥ç´ã¥ãã¦ããTagãåå¾ã§ãã¾ãã
user = User.find(1) tag = user.tag
Postã¢ãã«ãUserã¨Tagã®éã®ä»²ä»ã¢ãã«ã¨ãã¦æ©è½ãã¦ãã¾ãã Postã¢ãã«ãä»ãã¦Userã¨Tagéã®éæ¥çãªä¸å¯¾ä¸é¢ä¿ã確ç«ãã¦ãã¾ãã
has_many :through
has_many :through
ã¯ã1対å¤ã®ã¤ãªããã®2ã¤ã®ã¢ãã«ã®éã«ç¬¬3ã®ã¢ãã«ãç´ã¥ãã¦ããããããçµç±ãã¦ç¸æã¢ãã«ã®è¤æ°ã®ã¤ã³ã¹ã¿ã³ã¹ã¨ããããã¾ãã
1人ã®å¦çãè¤æ°ã®ææ¥ãåè¬ãã1ã¤ã®ææ¥ã«è¤æ°ã®å¦çãåå ããã¨ãããã¿ã¼ã³ãèãã¾ãã ãã®å ´å以ä¸ã®ããã«æ§é ã表ç¾ãããã¨ãåºæ¥ã¾ãã
class Student < ApplicationRecord has_many :enrollments has_many :courses, through: :enrollments end class Enrollment < ApplicationRecord belongs_to :student belongs_to :course end class Course < ApplicationRecord has_many :enrollments has_many :students, through: :enrollments end
Enrollmentã¨ããä¸éãã¼ãã«ãä½æãã¾ãã
ä¸éãã¼ãã«ã«ã¯å¤é¨ãã¼ãããããè¨å®ãã¾ããä»åã®å ´åã«ã¯Studentã¨Courseã®å¤é¨ãã¼ãããããç»é²ãã¦å¯¾è±¡ã®ãã¼ã¿ãçµãè¾¼ããããã«ãã¾ãã
Studentã¨Courseã¯ä¸éãã¼ãã«ã¨1対å¤ã®é¢ä¿ã«ãªã£ã¦ãã¦ãEnrollmentã¨ããä¸éãã¼ãã«ãçµç±ãã¦ä»ãã¼ãã«ã®ãã¼ã¿ãåå¾ãã¾ãã
ä¸è¨æ¹æ³ã§çå¾ãåãã¦ããã³ã¼ã¹ã®ä¸è¦§ãåå¾ã§ãã¾ãã
student = Student.find(1) courses = student.courses
has_one :throughã®å ´åã«ã¯ãPostã¢ãã«ãUserã¨Tagã®éã®ä»²ä»ã¢ãã«ã¨ãã¦æ©è½ãã¦ãã¾ããã
has_many :throughã®å ´åã«ã¯ãthroughã§æå®ãããPostã¢ãã«ãUserã¨Tagã®éã®ä¸éãã¼ãã«
ã¨ãã¦æ©è½ãã¦ãã¾ãã
ä¸éãã¼ãã«ã¯ãã¡ãã®è¨äºã§èª¬æãã¦ããã®ã§ãåããã¦ç¢ºèªãã¦ã¿ã¦ãã ããï¼
has_and_belongs_to_many
has_and_belongs_to_many
ã¯ã¢ãã«å士ãå¤å¯¾å¤ã®é¢ä¿ã®å ´åã«ä½¿ç¨ãã¾ãã
has_many :throughã¨ã¯éããä¸éãã¼ãã«ãä½æãã¾ããã has_many :throughã§ä½¿ç¨ããStudentã¨Courseã®ä¾ã§èãã¾ãã
class Student < ApplicationRecord has_and_belongs_to_many :courses end class Course < ApplicationRecord has_and_belongs_to_many :students end
Studentã¯è¤æ°ã®Courseãåãã¦ãCourseã¯è¤æ°ã®Studentãåå ãã¦ããã¨ããå¤å¯¾å¤ã®é¢ä¿ãhas_and_belongs_to_many
ã§è¡¨ãã¦ãã¾ãã
ããã§ãhas_many :throughã¨has_and_belongs_to_manyã¯åãé¢é£ä»ãããã¦ãããã¨ããããã¾ãããã©ã¡ããé¸æããæ¹ãããã§ããããï¼
has_many :throughã¨has_and_belongs_to_manyã®å¤§ããªéãã¯ä¸éã¢ãã«ãåå¨ãããã©ããã§ãã
has_and_belongs_to_many ã®å ´åãä¸éã¢ãã«ã¯åå¨ãã¦ãã¾ããã ãã®ããããçµåç¨ã®ãã¼ãã«ãåå¨ããã®ã§ãããçµåç¨ã®ãã¼ãã«ã¯å¤é¨ãã¼ã®ã¿ç»é²ããããã®ã§ãã
has_many :through ã¯ã¢ãã«ã«æ å ±ãå ¨ã¦æ示çã«æ®ããã¦ãã¾ãã ã¾ããä¸éãã¼ãã«ãä½æããã®ã§ãããã«å¤é¨ãã¼ä»¥å¤ã®æ å ±ãå«ãããã¨ãå¯è½ã§ãã(ã«ã¹ã¿ãã¤ãºããããã§ã)
å人çã«ã¯has_many :throughã§ä¸éãã¼ãã«ãä½æããæ¹æ³ã§åªå çã«èãããã¨æã£ã¦ãã¾ãã
çç±ã¨ãã¦ã¯ãã¾ããid(ãã©ã¤ããªã¼ãã¼)ãããããäºãã§ããªãã®ã§ãindexãã¯ããã¨ãã§ããªãã®ã§ãã¬ã³ã¼ãéãå¤ãã¨ãæéãããå¯è½æ§ãããããã§ãã ããã¦ãéçºãã¦ããä¸ã§ãå°ãªãã¨ãçµåãã¼ãã«ã®å±æ§ãå¢ããå¯è½æ§ã¯ååã«ãããã¾ããã§ãhas_and_belongs_to_many ã¯ã«ã¹ã¿ãã¤ãºã§ããªãã®ã§ä¸å®ã§ãã
ã¾ã¨ã
é¢é£ä»ãã®æ¹æ³ãããã¤ã説æãã¾ããã DBãã¢ãã«ã®è¨è¨ããã¦ããä¸ã§ã©ã®æ¹æ³ãé¸æãããã¯æ±ºã¾ã£ã¦ããã¨æãã¾ãã®ã§ãããã¾ããªæ¹æ³ãç¥ã£ã¦ããå¿ è¦ãããã¨æãã¾ãã