Ruby On Rails--Active Record Associations(关联关系)

Why do we need associations between models? Because they make common operations simpler and easier in your code.

Active Record Associations知识总览

官方文档

为什么使用Associations ?

Associations的类型

  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many
  • 如何判断选择belongs_to还是has_one
  • 如何判断选择has_many :through 还是 has_and_belongs_to_many
  • Polymorphic Associations(多态关联, :polymorphic, :as)
  • Self Joins(自关联)

注意事项

  • Controlling caching(控制缓存)
  • Avoiding name collisions(避免命名冲突)
  • Updating the schema(更新数据库表结构)
    • 为belongs_to创建外键
    • 为has_and_belongs_to_many创建关联表(中间表)
  • Controlling association scope(控制关联的scope,必要时需要使用:class_name参数)
  • Bi-directional associations(双向关联,多份对象拷贝可能导致数据不同步,:inverse_of参数)

各个关联关系详解

belongs_to详解

  • belongs_to 添加了哪些方法
    • association(force_reload = false)
    • association=(associate)
    • build_association(attributes = {})
    • create_association(attributes = {})
    • create_association!(attributes = {})
  • belongs_to 有哪些可选参数
    • :autosave
    • :class_name
    • :counter_cache
    • :dependent
    • :foreign_key
    • :inverse_of
    • :polymorphic
    • :touch
    • :validate
  • belongs_to 的scopes
    • where
    • includes
    • readonly
    • select
  • 判断关联对象是否存在: association.nil? 方法
  • 关联对象何时被保存

has_one 详解

  • has_one 添加了哪些方法
    • association(force_reload = false)
    • association=(associate)
    • build_association(attributes = {})
    • create_association(attributes = {})
    • create_association!(attributes = {})
  • has_one 有哪些可选参数
    • :as
    • :autosave
    • :class_name
    • :dependent
    • :foreign_key
    • :inverse_of
    • :primary_key
    • :source
    • :source_type
    • :through
    • :validate
  • 判断关联对象是否存在: association.nil? 方法
  • 关联对象何时被保存

has_many 详解

  • has_many 添加了哪些方法
    • collection(force_reload = false)
    • 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 = {})
  • has_many 有哪些可选参数
    • :as
    • :autosave
    • :class_name
    • :dependent
    • :foreign_key
    • :inverse_of
    • :primary_key
    • :source
    • :source_type
    • :through
    • :validate
  • has_many 可用的 scopes
    • where
    • extending
    • group
    • includes
    • limit
    • offset
    • order
    • readonly
    • select
    • uniq
  • 关联对象何时被保存

has_and_belongs_to_many 详解

  • has_and_belongs_to_many 添加了哪些方法
    • collection(force_reload = false)
    • 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 = {})
  • has_many 有哪些可选参数
    • :association_foreign_key
    • :autosave
    • :class_name
    • :foreign_key
    • :join_table
    • :validate
  • has_many 可用的 scopes
    • where
    • extending
    • group
    • includes
    • limit
    • offset
    • order
    • readonly
    • select
    • uniq
  • 关联对象何时被保存

Association Callbacks(关联Callbacks)

  • before_add
  • after_add
  • before_remove
  • after_remove

Association Extensions(关联扩展, extending)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值