使用關聯式資料庫時,table 和 table 間以 key 來連結彼此,rails 中以 has_many, has_one, belongs_to ... 等方式來實踐。
一對多:has_many & belongs_to
一對一:has_one & belongs_to
多對多:has_and_belongs_to_many
要注意的是,belongs_to 後面接的通常是單數。若改成複數的話,必需手動設定 :foreign_key 和 :class_name 等等參數。
Reference:
http://www.ptt.cc/bbs/Ruby/M.1312141310.A.0B4.html
http://guides.rubyonrails.org/association_basics.html
一對多:has_many & belongs_to
一對一:has_one & belongs_to
多對多:has_and_belongs_to_many
要注意的是,belongs_to 後面接的通常是單數。若改成複數的話,必需手動設定 :foreign_key 和 :class_name 等等參數。
Reference:
http://www.ptt.cc/bbs/Ruby/M.1312141310.A.0B4.html
http://guides.rubyonrails.org/association_basics.html
留言
張貼留言