关联

作者: Lucien_d70a | 来源:发表于2018-05-08 00:28 被阅读0次

支持的六种关联

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

相关文章

  • 关联 不关联

    烟雨朦胧里 横生而发的一缕思绪 是否停驻在朦胧斑驳的一处 询问 那一日 这一日的光景 你可曾望见 世俗里 有顽固的...

  • Runtime 关联对象, 可在分类中添加属性

    Runtime 关联对象, 可在分类中添加属性 关联 API 如下 设置关联值 获取关联值 取消关联 关联策略

  • iOS~关联类型、关联值、关联对象

    1、Associated Type (关联类型) Practical Protocols with Associa...

  • 关联的关联472

    刚刚读到一个观点,关联是进步的天梯。于是我忍不住也想通过这个观点再向外关联思考。关联,如果是自己所理解的那样的话,...

  • 设计模式中常见的关系举例

    设计模式就是面向对象的一种数据结构 关联关系 关联可以是双向关联、单向关联,其中单向关联还可以包含一种特殊的关联就...

  • SQL Server 2016 表操作:多表关联查询

    INNER JOIN 交叉关联 LEFT JOIN 左关联 RIGHT JOIN 右关联

  • 关联

    有的人 走着走着就丢了 有的人无论多远 都丢不了 就像在大海游泳 身边的水 总是连着远处的水 如同河流,流过你和我...

  • 关联

    一个是伫立在塞纳河畔的巴黎铁塔, 一个是横跨海河的万国桥(解放桥), 两者相隔万水千山, 它们之间...

  • 关联

    沙漠,风,沙波涟漪 岁月,时光,额上皱纹

  • 关联

    几个学习的链接地址 ibm_umlcsdn_blog 关联是什么

网友评论

      本文标题:关联

      本文链接:https://www.haomeiwen.com/subject/ikjmrftx.html