關(guān)聯(lián)

支持的六種關(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, &#8230;&#8203;)
collection.delete(object, &#8230;&#8203;)
collection.destroy(object, &#8230;&#8203;)
collection=(objects)
collection_singular_ids
collection_singular_ids=(ids)
collection.clear
collection.empty?
collection.size
collection.find(&#8230;&#8203;)
collection.where(&#8230;&#8203;)
collection.exists?(&#8230;&#8203;)
collection.build(attributes = {}, &#8230;&#8203;)
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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評論 0 10
  • 在開發(fā)中常常會涉及到多個模型進行關(guān)聯(lián)的操作.Rails支持六種關(guān)聯(lián): 為了后續(xù)的內(nèi)容分析,事先創(chuàng)建以下模型 bel...
    李傲娢閱讀 763評論 1 1
  • Hibernate對集合屬性的操作 持久化對象的映射集合屬性:Set List Collection Map So...
    WesleyLien閱讀 261評論 0 0
  • 文*云霞伴彩虹* 陽光穿透云霄,微微泛起波瀾,朦朧羞澀的露出笑臉,俏皮嬉戲。...
    云霞伴彩虹閱讀 623評論 1 3
  • 經(jīng)常思索,不斷反省: 正是因為知道自己有缺點,才沒有缺點。 天下最柔弱的東西,騰越穿行于最堅硬的東西中;無形的力量...
    M_152閱讀 167評論 0 0

友情鏈接更多精彩內(nèi)容