关联

支持的六种关联

belongs_to 这里写了以后会在对应的表中种下相应的关键字,语意其实是 我归属于 谁 例如 task belongs_to course task里面就有 course_id 代表 这个task 归属于哪个课程
has_one 我拥有谁,这里不会种下关键字,单纯的一对一关系

belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many

belongs_to 关联

class Book < ApplicationRecord
  belongs_to :author
end
# 每本书 归属于 一个作者
# book 中多了一个外键 叫做 author_id
# 立即获得方法
association
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})

# ------- 对应就是
author
author=
build_author
create_author
create_author!

# ------ 用法
# 例如 对应的关联是
class Author < ApplicationRecord
  has_many :books
end

# 我们就可以
@book.author
@book.author = @author
# 这两个区别在于 一个 不存入数据库 一个存入数据库
@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 关联

class Supplier < ApplicationRecord
  has_one :account
end
# 每个供应商拥有一个账户

has_many 关联

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 = {})

# 如果关联是
class Book < ApplicationRecord
  belongs_to :author
end

# 就可以
@books = @author.books
@author.books << @book1 (添加对象)
@author.books.delete(@book1)
@book_ids = @author.book_ids (返回数组 id)
@author.books.clear 
@author.books.empty? (是否存在)
@book_count = @author.books.size (返回集合中的对象数量。)
@available_books = @author.books.find(1)
@available_books = @author.books.where()
@available_books = @author.books.exists?() 检查是否有符合对象的条件

# 和belongs_to 一样 只是存入 和 不存入数据库的区别
@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 关联

  • 一般用于多对多 中间来个中间键
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
# 人通过联系可以联系到朋友
# 多个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
# 一篇文章拥有多个部分
# 一篇文章拥有多个段落 通过 部分

# 一个部分归属于一个文章
# 一个部分有多个段落

#一个段落归属于一个部分
# 然后我们就能通过 @document.paragraphs 获取这个document 中 所有的段落了

has_one :through 关联

  • 多用于一对一关系,中间来个中间键
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
# 一个供应商 拥有一个 账户
# 一个供应商 拥有一个 账户历史 通过 账户

# 一个账户 归属于一个 供应商
# 一个账户 拥有一个 账户历史

# 一个账户历史 关联一个 账户

多态关联

  • 关联还有一种高级形式——多态关联(polymorphic association)。在多态关联中,在同一个关联中,一个模型可以属于多个模型。
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 图片集
# 产品 拥有 多个 图片 as 图片集

@product.pictures 可以获取产品的图片
@picture.imageable 获取这张图片属于谁的(父对象)

自联结

class Task < ApplicationRecord
  belongs_to :created, class_name: ‘User’, foreign_key: ‘created_id’
end
# 用来语意化
# 每个任务 归属 一个创建者
# 这个创建者就是 对应User 表
# 在task关联的外键就是created_id

完

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,232评论 0赞 10
  • 在开发中常常会涉及到多个模型进行关联的操作.Rails支持六种关联: 为了后续的内容分析,事先创建以下模型 bel...
    李傲娢阅读 805评论 1赞 1
  • Hibernate对集合属性的操作 持久化对象的映射集合属性:Set List Collection Map So...
    WesleyLien阅读 295评论 0赞 0
  • 文*云霞伴彩虹* 阳光穿透云霄,微微泛起波澜,朦胧羞涩的露出笑脸,俏皮嬉戏。...
    云霞伴彩虹阅读 631评论 1赞 3
  • 经常思索,不断反省: 正是因为知道自己有缺点,才没有缺点。 天下最柔弱的东西,腾越穿行于最坚硬的东西中;无形的力量...
    M_152阅读 180评论 0赞 0

友情链接更多精彩内容