Rails 5 开发进阶
  • Rails 6 开发进阶
  • Active Model 功能模块
    • Model - 核心
      • Validations
      • Validator
      • Errors
      • Validations 相关的 Callbacks
      • Conversion
      • Naming & Name
      • Translation
      • Lint Tests
    • Model 的增强模块
      • Attribute Assignment
      • Attribute Methods
      • Dirty
      • Secure Password
      • Forbidden Attributes Protection
      • Serialization
      • Callbacks - 快速提供 3 个回调方法
    • 其它
  • Active Record 数据库操作:增删查改
    • Relation
      • Relation 文件下的方法
      • Query Methods
        • Preload, Eagerload, Includes 和 Joins 等
        • Query Methods 读取、设置查询方法
        • 关联表复杂查询示例
      • Spawn Methods
      • Batches 批量查询
      • Finder Methods
      • Calculations 统计计算
      • 其它
    • Collection Proxy
    • Scoping
    • Attribute Methods
      • Attribute Methods 文件下的内容
      • Read & Write
      • Before Type Cast - 类型转换
      • Query - 后缀 '?' 问询
      • Serialization
      • Primary Key
      • 其它
    • Persistence
      • 数据更新方法对比
      • 对比,然后使用合适的方法
      • 多个 save 方法
    • Counter Cache 计数器
    • Querying
    • 其它
  • Active Record 数据库关联关系
    • Associations 文件 - 4 个关联方法
    • Aggregations - composed_of 方法
    • Builder - 功能的实现
      • Association
      • Singular Association
      • Collection Association
      • Has One
      • Belongs To
      • Has Many
      • Has And Belongs To Many
    • Reflection - 实现之关联两者
      • Abstract Reflection
      • Macro Reflection
      • Association Reflection
      • Aggregate Reflection
      • Has Many Reflection
      • Has One Reflection
      • Belongs To Reflection
      • Has And Belongs To Many Reflection
      • Through Reflection
      • Polymorphic Reflection
      • Runtime Reflection
    • Association 目录 - 实现之提供方法
      • Association
      • Belongs To Association
      • Belongs To Polymorphic Association
      • Collection Association
      • Foreign Association
      • Has Many Association
      • Has Many Through Association
      • Has One Association
      • Has One Through Association
      • Singular Association
      • Through Association
    • 4 个关联方法的使用
      • belongs_to
      • has_one
      • has_many
      • has_and_belongs_to_many
      • 关联方法的可选参数汇总
    • 4 个关联方法的补充
      • 使用关联方法后,Rails 自动生成了哪些方法
      • habtm vs has_many through
    • Nested Attributes 嵌套属性
    • Inheritance 单表继承
    • 其它
      • Autosave Association
      • Alias Tracker
      • Association Scope
      • Join Dependency
      • Preloader
      • Association Relation
  • Active Record 数据库特性及功能模块
    • Callbacks 回调
      • 回调及其顺序
      • 跳过回调
      • 可选参数
      • after_create 与 after_commit on create 各自存在的坑
    • Transactions 事务
    • Locking 锁
    • Enum 枚举
    • Store
    • Validations 校验
    • Secure Token
    • Integration
    • No Touching
    • Touch Later
    • Attributes
    • Readonly Attributes
    • Serialization
    • Suppressor
  • Active Record 数据库迁移:表/字段
    • Connection Handling 连接数据库
    • Migration 文件下的内容
      • 控制台里迁移、回滚等命令
      • say_with_time
    • Connection Adapters
      • Schema Statements
        • create_table
        • change_table
      • Table Definition
      • Table
      • Database Statements
    • Model Schema
    • Schema
    • 其它
      • schema_format
      • Schema Migration
  • Active Record 数据库连接:底层适配,约定、配置
    • Naming Conventions
    • Schema Conventions
    • Timestamps
    • Core
      • 获取 record 对象
    • Sanitization
    • Migration DatabaseTasks
    • Statement Cache
    • Collection Cache Key
    • Attribute Mutation Tracker
    • Attribute Decorators
    • 其它
  • Action View 布局、渲染相关
    • Layouts
    • Base - 为渲染打基础
    • 非 Rails 是如何渲染的
    • Rails 是如何渲染的
      • Rendering
    • 渲染器介绍
      • Renderer - 渲染的入口
      • 基类 Abstract Renderer
      • 子类 Template Renderer
      • 子类 Partial Renderer & Collection Caching
      • 子类 Streaming Template Renderer
    • Template 内容
      • template 本文件下的内容
      • template 目录
    • Lookup Context
      • View Paths
      • Details Key & Details Cache
    • view_context
      • Context
      • Output Flow & Streaming Flow
    • View Paths
    • 其它
      • Output Buffer & Streaming Buffer
  • Action View 提供的 Helper 方法
    • 与表单直接相关的辅助方法
      • Form Builder
        • 扩展 Form Builder
      • Form Helper
      • Form Options Helper
      • Form Tag Helper
    • 与表单非直接相关的辅助方法
      • Tag Helper
      • Url Helper
      • Asset Tag Helper
      • Cache Helper
        • 片段缓存和 Cache Key
      • Controller Helper
      • Asset Url Helper
      • Csrf Helper
      • Capture Helper
      • Debug Helper
      • Date Helper
      • JavaScript Helper
      • Number Helper
      • Output Safety Helper
      • Rendering Helper
      • Record Tag Helper
      • Sanitize Helper
      • Translation Helper
      • Text Helper
      • Atom Feed Helper
      • Active Model Instance Tag
    • 其它
  • Action View 其它类和模块
    • Record Identifier
    • Routing Url For
    • Model Naming
    • Digestor
      • Dependency Tracker
    • 其它
  • Action Dispatch Http 作用:请求、响应
    • Request
      • Request 文件下的内容
      • URL
      • Headers
      • Mime Negotiation
      • Parameters
      • Parameter Filter & Filter Parameters
      • Cache 之 Request
      • Uploaded File
      • Utils
    • Response
      • Response 文件下的内容
      • Filter Redirect
      • Cache 之 Response
    • 其它
      • Rack Cache
      • Mime Type register
      • Session
  • Action Dispatch Middleware 中间件
    • Middleware
      • Rack - Ruby Web server 接口
      • 查看项目用了哪些 Middleware
      • 定制 Middleware
      • Middleware Stack
    • 各个 Middleware 类
      • Static
      • SSL
      • Show Exceptions
      • Exception Wrapper
      • Public Exceptions
        • 定制 Public Exceptions
      • Debug Exceptions
      • Request Id
      • Remote Ip
      • Reloader
      • Params Parser
      • Flash
      • Cookies & ChainedCookieJars
      • Session
      • Callbacks
      • Load Interlock
      • Executor
      • Debug Locks
  • Action Dispatch Routing 路由顶层(表现形式)
    • Mapper
      • Base
      • Http Helpers
      • Scoping
        • scope
        • namespace
      • Concerns
      • Resources
      • Scope、Mapping、Constraints
      • Redirection
      • match 和 scope 方法 - 重中之重
      • 路由常用方法汇总
    • 其它
      • Polymorphic Routes
      • Url For
      • Routing 概述:生成、存储、识别
      • 一步步分析从请求到响应涉及到 Rails 的哪些模块
      • endpoint 和 inspector 文件
  • Action Dispatch RouteSet 路由底层(实现方式)
    • 实例对象和各个实例方法
    • Named Route Collection
    • Dispatcher
    • Generator
    • Routes Proxy
    • Journey
  • Abstract Controller
    • Base
      • 获取所有的 Controller 和 action
    • Helpers
    • Callbacks
    • Rendering
    • render 参数汇总与详解
      • 更多关于渲染
    • Translation
    • Collector
    • 其它
      • Asset Paths
      • Routes Helpers
      • Logger
      • Url For
    • Caching
    • Caching & Caching Fragments
      • 默认片段缓存策略
  • Action Controller - Rack 增强扩展
    • Metal - 增强的 Rack, 简陋的 Controller
      • Metal 文件下的内容
      • Middleware Stack
      • Metal 使用举例
    • API - Metal 的继承者
    • Base - Metal 的继承者
    • Metal 的扩展
      • Redirecting
      • Head
      • Conditional Get - HTTP Cache
        • Conditional Get 其它
      • Etag With Template Digest
      • Http Authentication
        • Basic
        • Digest
        • Token
      • Streaming
      • Live
      • Mime Responds & Collector
      • Renderers 增删渲染器
      • Params Wrapper
      • Request Forgery Protection
      • Parameters & Strong Parameters
      • Data Streaming
      • Force SSL
      • Flash
      • Helpers
      • Cookies
      • Implicit Render
      • Instrumentation
      • Rendering
      • Rescue
      • Url For
      • 其它
    • 其它
      • Form Builder
      • Renderer
      • Basic Implicit Render
      • Railties Helpers
  • Active Support 扩展:autoload 的类和模块
    • Autoload
      • autoload & eager_autoload 补充
    • Lazy Load Hooks
    • Concern
    • File Update Checker
    • Notifications
      • Rails 默认已有的 instrumenter
    • Subscriber
    • Log Subscriber
      • Action Mailer Log Subscriber
      • Action Controller Log Subscriber
      • Action View Log Subscriber
      • Active Record Log Subscriber
    • Rescuable
    • Descendants Tracker
    • Dependencies
  • Active Support 扩展:eager_autoload 的类和模块
    • Cache 缓存的源头
    • Callback 方法解释及使用
    • Callbacks 底层简要分析
    • Configurable
    • Ordered Hash 和 Ordered Options
    • Inflector
    • Key Generator 和 Caching Key Generator
    • Message Encryptor 和 Message Verifier
    • String Inquirer - Rails.env.production?
    • Array Inquirer
    • Tagged Logging
    • Gzip 与 JSON
    • Backtrace Cleaner
    • Number Helper
    • Benchmarkable
    • Xml Mini
    • Multibyte
    • Deprecation
    • 其它
  • Active Support 核心扩展
    • Array
    • Benchmark
    • Big Decimal
    • Class
    • Date
    • Time
    • Date Time
    • Duration
    • Time With Zone
    • Time Zone
    • Enumerable
    • File
    • Hash
    • Hash With Indifferent Access
    • Integer
    • Numeric
    • Kernel
    • Object
    • Module
      • alias_method_chain
    • Marshal
    • Range
    • Regexp
    • Secure Random
    • String
    • URI
    • Load Error
    • Name Error
    • Logger
    • Logger Silence
  • Active Support 其它类和模块
    • Security Utils
  • railties 核心:初始化、组件化
    • Railtie
      • Railtie 文件下的内容
      • Initializable
      • Configuration
      • 定制自己的 Railtie
    • Railtie 补充
      • Rails 默认组件都是 Railtie
    • Engine
      • Engine 文件下的内容
      • Configuration
      • Engine full vs mountable
      • 定制自己的 Engine
      • 其它
    • Application
      • Application 文件下的内容
      • Bootstrap
      • Finisher
      • Configuration
      • Default Middleware Stack
      • Rails 应用启动过程
    • Application 补充
      • Routes Reloader
      • rake & rails 命令
      • rails console 里的小技巧
      • 路径 - Root 和 Path
      • 提示信息页面
    • 其它
      • Rails 文件下的内容
      • Configuration Middleware Stack Proxy
      • AppName, Application. Engine, Railtie
      • Backtrace Cleaner
      • Rack Logger
      • Generators
  • Rails Configuration 配置
    • 一般常用配置项
    • Action Mailer
    • Active Record
    • Action Controller
    • Action Dispatch
    • Action View
    • Active Support
    • I18n
    • Generators
    • Assets
    • Environments
    • 设置项补充
    • Middleware
  • Rails Generators 代码生成器
    • Thor
    • generators 下的目录、文件
    • Base
    • Actions
    • Named Base
    • 其它
      • App Base
      • Generators 文件下的内容
      • Migration
      • Active Model
      • Resource Helpers
      • Model Helpers
      • Generated Attribute
  • Action Mailer 发邮件
    • Base
      • 拦截器 register_interceptor
      • 订阅者 register_observer
    • Mail Helper
    • Message Delivery
    • 其它
      • Delivery Methods
      • Previews & Preview
      • DeliveryJob
      • Collector
  • Active Job 异步任务
    • Active Job 使用
    • Active Job 补充
  • Action Cable 封装的 WebSockets
    • 服务端 - Ruby 代码
      • Channel
      • Channel 补充
      • Connection
      • Connection 补充
      • Server
      • Server 补充
      • Remote Connections
      • Action Cable Helper
      • Subscription Adapter
      • Subscription Adapter 补充
    • 客户端 - Coffee 脚本
      • Action Cable 文件下的内容
      • Consumer
      • Subscription
      • Connection
      • Subscriptions
      • Connection Monitor
    • 一些重要的东西
    • 其它
  • Active Storage 存储功能最小实现
  • 前端 Turbolinks
  • 前端 Webpacker
  • 测试 Testing
    • Active Support
    • Active Job
    • Action Mailer
    • Action Controller
    • Action Dispatch
    • Action View
    • Generators
    • rails-dom-testing
  • 测试 MiniTest
    • Spec DSL
    • Assertions
    • Mock
    • Unit
      • TestCase
      • Lifecycle Hooks
      • Guard
    • Expectations
    • 其它
  • 测试 Factory Girl
  • rails-guides 指南
  • 其它
    • 继承心得
    • Rails 源代码里一些常用方法
    • Rails assets precompile
    • Turbolinks 产生的原因
    • Turbolinks 3
    • Turbolinks 补充
    • Turbolinks 5
  • 后记
由 GitBook 提供支持
在本页
  • 关联方法的可选参数汇总
  • belongs_to
  • has_one
  • has_many
  • has_and_belongs_to_many
  • 实现关联对象:
  • 详解:
  • 心得

这有帮助吗?

  1. Active Record 数据库关联关系
  2. 4 个关联方法的使用

关联方法的可选参数汇总

关联方法的可选参数汇总

belongs_to

valid_options

ActiveRecord::Associations::Builder::BelongsTo.valid_options nil

valid_dependent_options

ActiveRecord::Associations::Builder::BelongsTo.valid_dependent_options

has_one

valid_options

ActiveRecord::Associations::Builder::HasOne.valid_options through: 'fakers'

valid_dependent_options

ActiveRecord::Associations::Builder::HasOne.valid_dependent_options

has_many

valid_options

ActiveRecord::Associations::Builder::HasMany.valid_options nil

valid_dependent_options

ActiveRecord::Associations::Builder::HasMany.valid_dependent_options

has_and_belongs_to_many

和 has_many 一样。

实现关联对象:

参数

belongs_to

has_one

has_many

habtm

:class_name

√

√

√

√

:foreign_key

√

√

√

√

:foreign_type

√

√

√

√

:primary_key

√

√

√

√

:as

√

√

√

:through

√

√

√

:source

√

√

√

:source_type

√

√

√

:join_table

√

√

:association_foreign_key

√

:table_name

√

√

:autosave

√

√

√

√

:dependent

√

√

√

√

:before_add

√

√

:after_add

√

√

:before_remove

√

√

:after_remove

√

√

:validate

√

√

√

√

:required

√

√

:counter_cache

√

√

√

:polymorphic

√

:touch

√

:inverse_of

√

√

√

√

:anonymous_class

√

√

√

√

:optional

√

:extend

√

√

:index_errors

√

√

详解:

class_name

指定关联对象所对应的"类/Model",默认是"驼峰"转换关联对象的名字。
不符合约定时,用 :class_name 指明。

:class_name 不影响 :foreign_key "关联对象_id"属性的命名约定。

foreign_key

belongs_to - 默认是"关联对象_id",外键存在在自己表里。

has_many 等 - 声明自己在关联对象里的外键。

primary_key

查询关联对象的时候,用自己的哪个字段做为条件。
指定关联对象的"主键",保存在关联对象的表里,查询关联对象时用到。查询关联对象时,自己保存的外键对应着关联对象的主键(默认是 id),如果不符合约定,可以用 :primary_key 指明。

凡是通过前者查询后者。传递的是前者的主键所对应的值,也就是 id 字段的值,如果觉得不合适,可以用 :primary_key 指明字段。

as

多态时用到,声明多态的接口。和 belongs_to 里的 :polymorphic 配对使用。

through

在中间表里,希望关联对象怎么被表示。(听起来是不是有点绕,管得也太多了吧)
指定中间表。优先级比 :class_name、:primary_key 和 :foreign_key 要高。
实现关联时用到了 reflection 的代码,所以 has_one 或 belongs_to 关联要使用中间表,只能通过 :through 这一种方式,区别于 has_many :through 和 has_and_belongs_to_many.

指定中间表。优先级比 :class_name, :primary_key 和 :foreign_key 要高。<br> 如果对应的关联是 belongs_to 则,关联表会被自动更新、创建、删除。否则,关联表为只读状态,也就是说创建后你就只能手动维护,不会自动更新、删除。 <br> 如果你要更改关联,最好配置一下 :inverse_of 选项,以便被关联对象及时更新与其父亲的关系。

source

必须和 :through 配合使用,自己在中间表里用什么表示。(类似 :as)

配合 :through 使用,当查询关联表数据时用哪张表的字段。例如 has_many :subscribers, through: :subscriptions,如果不指定,默认会查询 :subscribers 或 :subscriber 表

中间表关联着前者和后者,并且"前者.后者"可拆分成1)"前者.中间表",2)"中间表.后者"。第 1 步一般不会有误,但如果后者名字不规范,那么在第 2 步"中间表.后者"就会走不下去。用 :source 明确后者对应中间表里的什么关联。

source_type

必须和 polymorphic 配合使用。多态时希望自己用什么做为类型。
影响的是数据,不是属性。

从后者的角度来看,后者与前者的关联应该是 belongs_to. 但如果恰好又是多态,那么后者保存有前者的 id 并指定某个类型. 如果你对按约定生成的类型不满意,可以用 :source_type 指明。

foreign_type

1 对于 has_one 和 has_many,用什么字段做为自己的外键。<br>
2 对于 belongs_to,用什么字段做为关联对象的外键,字段保存在前者。

指定用什么"字段"保存关联对象的"多态信息",保存在自己的表里,多态时用到。默认用"关联对象_type"这个字段,不符合约定时,用 :foreign_type 指明。

没有这个选项之前,这个字段只能根据 `:as` 生成,不能自定义。

自己不符合约定。多态时,在关联对象的表里,用什么字段来存储父亲对象的类型(默认是 x_type,根据 :as 而来)

多态时,在关联对象的表里,用什么字段来存储父亲对象的类型(默认是 x_type,根据 :as 而来) 

optional

belongs_to 默认会检测其关联的对象是否存在,如果不存在则不能保存自己。设置为 false 后,则没有上述特性。

extend

作用和 extension 类似。

join_table

中间表。

指定中间表的名字 **注意:** 如果你给中间表加了对应的'类',并且命名不符合约定的话。那么一定要记得在每个 has_and_belongs_to_many 的地方都要设置 join_table

- 注意前者与后者的顺序比较方法,如果前几个字符一样,则往后一个个字符比较,如 "paper_boxes" 和 "papers" 生成的中间表名字是 "paper_boxes_papers" 而不是 "papers_paper_boxes". 
- 如果前者和后者使用的表名都带有前缀并且还相同,那么中间表的名字也用同样的前缀,剩余部分用再按字母顺序排序。如:"catalog_categories" 和 "catalog_products" 生成的中间表是 "catalog_categories_products".

association_foreign_key

在中间表里,希望关联对象用什么字段做为外键。(听起来是不是有点绕,管得也太多了吧)

指定后者所对应的外键,如 Person has_and_belongs_to_many :projects, 则中间表里后者所对应的外键是 “project_id”。如果不符合要求,你使用使用 :association_foreign_key 设置

作用:相当于 has_and_belongs_to_many 时 belongs_to 部分的 foreign_key

autosave

保存自己时,自动保存关联对象。
如果在 model 里使用了 accepts_nested_attributes_for,则对应 :autosave 始终为 true.

设置为 true, 保存自己的时候,同时保存它的关联对象(用的是 before_save)。设置为 false 还可分为两种情况:前者为 new_record,则保存自己时会自动保存关联对象;否则上述自动操作都不会被执行。

设置为 true,保存父亲对象时,其关联对象同时被保存。设置为 false, 对关联对象不做任何操作。默认,只有被关联对象为 new_record 时才会自动保存。<br> 使用 accepts_nested_attributes_for 会自动设置 :autosave 为 true.

以 before_save 的形式来调用,所以会受到其它 before_save 回调方法的影响。

dependent

1 belongs_to 只有 destroy 和 delete<br>
2 has_many 有 destroy、delete_all、nullify、restrict_with_exception 和 restrict_with_error<br>
3 has_one  有 destroy、delete、nullify、restrict_with_exception 和 restrict_with_error

  - `:destroy` 删除(destroy)所有被关联对象。

  - `:delete_all` 和 destroy 类似,也是删除所有被关联对象。但区别在于,此删除操作不会触发回调。

  - `:nullify` 设置后者的"前者_id"属性为 nil. 不会触发回调。

  - `:restrict_with_exception` 有关联对象则抛异常。并且后面与之的无关代码也不能再运行。

  - `:restrict_with_error` 有关联对象则设置对象的 errors 信息。并且后面与之无关的代码还能运行。

如果设置为 :destroy, 自己被 destroy 时,关联对象会被 destroy. 如果设置为 :delete, 自己被 destroy 时,关联对象会被 delete. 注意:自己被 delete, 始终不影响关联对象。

删除前者时,对后者进行什么操作。1) :destroy, 删除后者,会触发回调;2) :delete, 删除后者,不会触发回调;3) ::nullify, 把后者里的"前者_id"属性设置为 nil, 不会触发回调;4) :restrict_with_exception, 如果有后者的关联对象,报异常;5) :restrict_with_error 如果有后者的关联对象,报错

可选 :destroy,也就是使用 destroy 删除所有关联对象;可选 :delete_all,也就是使用 delete 删除所有关联对象;可选 :nullify,把外键设为 nil,但不删除对象;可选 :restrict_with_exception,有关联对象则抛异常;可选 :restrict_with_error,有关联对象则抛错误

当关联对象与自己的关系是 has_many 时,请慎用 `:dependent`. 因为关联对象被同时删除的话,意味着自己的兄弟将成为孤儿(没有关联对象可关联)。

validate

保存自己的时候,校验内存里的关联对象(不是相应字段)是否存在于数据库里。

1 设置了 validate<br>
2 不设置 validate,但设置了 autosave, 或 has_many 和 habtm 关联。<br>
3 方法名是:autosave_association 里的 :"validate_associated_records_for_#{关联对象}"<br>
4 内容是 :validate_collection_association 或 :validate_single_association<br>
5 后面使用 validate 进行执行<br>
6 校验关联是否成立,并且关联对象是否存在。

类似 validate_presence_of :关联对象

设置为 true, 保存自己的时候,会先校验它的关联对象。默认是 false, 也就是不校验。

对于是否是 new_record 在做 valid? 时,会造成迷惑。牢记,`:validate` 意味着对前者和后者都有"保存"操作。如果校验失败,则本次保存失败。

校验关联对象是否真实存在于数据库里。设置为 false, 保存前者时不会校验后者。默认就是 false.

inverse_of

通过自己查找到关联对象,然后又通过关联对象找回自己。

有的关联会自动推断 inverse_of,所以用不用其实效果一样。而有的关联加上参数后,不起作用,所以即使设置也没用。可以根据以下代码进行检测:

ModelName.reflections.map do |key, value|
  p "#{key} inverse_of: #{value.has_inverse?}"
end

counter_cache

分为几种情况:

1 belongs_to 里设置为 true 表明使用 counter. (字段使用默认)<br>
2 belongs_to 里设置 counter 字段。<br>
3 has_many   里设置 counter 字段。

设置为 true 后,自己被创建或删除,会改变关联对象里"计数器的值"。默认是 false,也就是不起作用。

计数器是根据"前者对应的 table_name + count"生成的,如里不符合"约定",可以用 :counter_cache 指明。自定义计数器名字的时候,建议把此属性声明为 attr_readonly. 综上,:counter_cache 可以设置为 true、false、或自定义的名字。

定制用什么字段保存关联表的统计数目

touch

1 设置为 true,更新自己后,更新关联对象的 updated_at/on 字段。<br>
2 设置为其它字段,则更新自己后,额外更新关联对象的 updated_at/on 和其它字段。

设置为 true, 保存或 destroy 自己的时候,关联对象的 updated_at/on 属性会被更新。

如果你不是设置成 true, 而是传递一个符号 :symbol,那么这个符号会被更新为当前时间。

required

要求有关联对象存在于数据库。

语法糖。原来的做法是 "belongs_to 关联对象" + "validates_presence_of 关联对象",现改为 "belongs_to 关联对象, required: true"

"validates_presence_of 关联对象" 和 "validates_presence_of 关联对象_id" 大同小异。前者是真实存在的对象,后者只是字段。

readonly

所有关联对象为只读。

设置为 true, 通过前者查询到的后者限制为只读状态,不可更改。但其它方式查询出来的,不受此限制。

polymorphic

声明此关联是多态的

如果你同时使用了 `:counter_cache`,建议在后者的 model 里把计数器设置为 attr_readonly.

inverse_of

保证 object_id 相同。通过前者(1)查询到后者,然后再通过后者返过来查询前者(2)。按照直观的理解,(1) 和 (2) 应该是同样的对象,同样的值。但实际情况会发现,它们不一样(可以通过 object_id 确定)!原因是程序没有这么聪明,没法判断它们是一样的(特别是通过中间表查询时)。设置 :inverse_of,可解决这个问题。

带来额外的好处:1. 不用重复查询,节省了性能;2. 因为对象的 object_id 都是一样的,保证了数据一致性;3. 注意顺序前者查询后者,然后后者反向查询前者;需要注意:第 2 次"查询"不是数据库查询,会存在操作脏数据的风险。

通常情况下,不用设置,会自动转换。但使用了以下参数,则不会自动转换::through、:as、:polymorphic 和 :conditions;遇到单复数不规则,有时候也不会自动转换

index_errors

默认为 false. 设置了 autosave 它才管用。条件很复杂,忽略。

anonymous_class

Reflection 那边用到,忽略。

before_add、after_add、before_remove 和 after_remove

作用于集合,和普通的回调差不多,只是定义的位置不同而矣。

心得

关系比较复杂的时候,不好写。我建议先从简单、可确定的入手,然后进行下一步。

例如:自关联,或者通过关联表实现关联,通常:

  • 表里有 xxx_id 等外键的,通常就是 belongs_to

  • 然后对应其关联的就是 has_many

  • 再之后就是 through

上一页has_and_belongs_to_many下一页4 个关联方法的补充

最后更新于5年前

这有帮助吗?