Railsã§ãã¼ãã«ã®ãªã¬ã¼ã·ã§ã³ãã§ãããªãã¦ããã¯ãªã ã
ãã¾ãã«ã¡ãã¼ç°¡åãã¾ããããã¬ãã·ã§ããã³ã°ã®å¤äººå¸ä¼è
ãåºã¦ãã¦ããããªã«ç°¡åãªãã¦ä¿¡ãããããã¼ããã¸ã§ã³ã
1:1ã®å ´å
ã©ã¡ããä¸æ¹ã®ãã¼ãã«ã«ããã¼ãã«åã®åæ°å½¢ã_idã¨ãããã£ã¼ã«ããä½æããã
class Author < ActiveRecord:Base
belongs_to(:post)
end
class Post < ActiveRecord:Base
has_one(:author)
end
1:nã®å ´å
nå´ã®ãã¼ãã«ã«1å´ã®ããã¼ãã«åã®åæ°å½¢ã_idã¨ãããã£ã¼ã«ããä½æããã
class User < ActiveRecord::Base
belongs_to(:group)
end
class Group < ActiveRecord:Base
has_many(:users)
end
n:nã®å ´å
2ã¤ãã¼ãã«åãã¢ã«ãã¡ãããé ã«'_'(ã¢ã³ãã¼ã¹ã³ã¢)ã§ç¹ããååã®ä¸éãã¼ãã«ãä½æããããã«ããããã®ããã¼ãã«åã®åæ°å½¢ã_idã¨ãããã£ã¼ã«ããä½æããã
categories_projects |
category_id |
project_id |
class Category < ActiveRecord::Base
has_and_belongs_to_many(:projects)
end
class Project < ActiveRecord::Base
has_and_belongs_to_many(:categories)
end
ãã¼ãããconvention over configurationããå¼·ãå®æã