支持的六種關(guān)聯(lián)
belongs_to 這里寫了以后會在對應(yīng)的表中種下相應(yīng)的關(guān)鍵字,語意其實是 我歸屬于 誰 例如 task belongs_to course task里面就有 course_id 代表 這個task 歸屬于哪個課程
has_one 我擁有誰,這里不會種下關(guān)鍵字,單純的一對一關(guān)系
belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many
belongs_to 關(guān)聯(lián)
class Book < ApplicationRecord
belongs_to :author
end
# 每本書 歸屬于 一個作者
# book 中多了一個外鍵 叫做 author_id
# 立即獲得方法
association
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})
# ------- 對應(yīng)就是
author
author=
build_author
create_author
create_author!
# ------ 用法
# 例如 對應(yīng)的關(guān)聯(lián)是
class Author < ApplicationRecord
has_many :books
end
# 我們就可以
@book.author
@book.author = @author
# 這兩個區(qū)別在于 一個 不存入數(shù)據(jù)庫 一個存入數(shù)據(jù)庫
@author = @book.build_author(author_number: 123,
author_name: "John Doe")
@author = @book.create_author(author_number: 123,
author_name: "John Doe")
create_author! 和 上面一樣 只不過保存失敗會返回異常
has_one 關(guān)聯(lián)
class Supplier < ApplicationRecord
has_one :account
end
# 每個供應(yīng)商擁有一個賬戶
has_many 關(guān)聯(lián)
class Author < ApplicationRecord
has_many :books
end
# 每個作者可以擁有多本圖書
# 獲得這些方法
collection
collection<<(object, …​)
collection.delete(object, …​)
collection.destroy(object, …​)
collection=(objects)
collection_singular_ids
collection_singular_ids=(ids)
collection.clear
collection.empty?
collection.size
collection.find(…​)
collection.where(…​)
collection.exists?(…​)
collection.build(attributes = {}, …​)
collection.create(attributes = {})
collection.create!(attributes = {})
# 如果關(guān)聯(lián)是
class Book < ApplicationRecord
belongs_to :author
end
# 就可以
@books = @author.books
@author.books << @book1 (添加對象)
@author.books.delete(@book1)
@book_ids = @author.book_ids (返回數(shù)組 id)
@author.books.clear
@author.books.empty? (是否存在)
@book_count = @author.books.size (返回集合中的對象數(shù)量。)
@available_books = @author.books.find(1)
@available_books = @author.books.where()
@available_books = @author.books.exists?() 檢查是否有符合對象的條件
# 和belongs_to 一樣 只是存入 和 不存入數(shù)據(jù)庫的區(qū)別
@book = @author.books.build(published_at: Time.now,
book_number: "A12345")
@book = @author.books.create(published_at: Time.now,
book_number: "A12345")
collection.create!
has_many :through 關(guān)聯(lián)
- 一般用于多對多 中間來個中間鍵
class Person
has_many :contacts
has_many :friends, through: :contacts
end
class Friend
has_many :contacts
has_many :people, through: :contacts
end
class Contact
belongs_to :friend
belongs_to :person
end
# 人通過聯(lián)系可以聯(lián)系到朋友
# 多個friends ---- contacts ---- 多個Person
class Document < ApplicationRecord
has_many :sections
has_many :paragraphs, through: :sections
end
class Section < ApplicationRecord
belongs_to :document
has_many :paragraphs
end
class Paragraph < ApplicationRecord
belongs_to :section
end
# 一篇文章?lián)碛卸鄠€部分
# 一篇文章?lián)碛卸鄠€段落 通過 部分
# 一個部分歸屬于一個文章
# 一個部分有多個段落
#一個段落歸屬于一個部分
# 然后我們就能通過 @document.paragraphs 獲取這個document 中 所有的段落了
has_one :through 關(guān)聯(lián)
- 多用于一對一關(guān)系,中間來個中間鍵
class Supplier < ApplicationRecord
has_one :account
has_one :account_history, through: :account
end
class Account < ApplicationRecord
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ApplicationRecord
belongs_to :account
end
# 一個供應(yīng)商 擁有一個 賬戶
# 一個供應(yīng)商 擁有一個 賬戶歷史 通過 賬戶
# 一個賬戶 歸屬于一個 供應(yīng)商
# 一個賬戶 擁有一個 賬戶歷史
# 一個賬戶歷史 關(guān)聯(lián)一個 賬戶
多態(tài)關(guān)聯(lián)
- 關(guān)聯(lián)還有一種高級形式——多態(tài)關(guān)聯(lián)(polymorphic association)。在多態(tài)關(guān)聯(lián)中,在同一個關(guān)聯(lián)中,一個模型可以屬于多個模型。
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
# 圖片 它 歸屬于 圖片集
# 員工 擁有多個 圖片 as 圖片集
# 產(chǎn)品 擁有 多個 圖片 as 圖片集
@product.pictures 可以獲取產(chǎn)品的圖片
@picture.imageable 獲取這張圖片屬于誰的(父對象)
自聯(lián)結(jié)
class Task < ApplicationRecord
belongs_to :created, class_name: ‘User’, foreign_key: ‘created_id’
end
# 用來語意化
# 每個任務(wù) 歸屬 一個創(chuàng)建者
# 這個創(chuàng)建者就是 對應(yīng)User 表
# 在task關(guān)聯(lián)的外鍵就是created_id