Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reference consistency check correction (#45840)
* Reference consistency check correction 

By default all belongs_to relations are set to required, and required means Rails checks the reference consistency. That sentence is correct only if the optional is set to true, then in this case Rails not only allows this field to be null but also it does not check the reference consistency. But this is not the default behavior.

Co-authored-by: Eileen M. Uchitelle <eileencodes@users.noreply.github.com>
  • Loading branch information
sizief and eileencodes committed Aug 18, 2022
1 parent 487282c commit c13856e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion guides/source/association_basics.md
Expand Up @@ -125,7 +125,7 @@ end
When used alone, `belongs_to` produces a one-directional one-to-one connection. Therefore each book in the above example "knows" its author, but the authors don't know about their books.
To setup a [bi-directional association](#bi-directional-associations) - use `belongs_to` in combination with a `has_one` or `has_many` on the other model.

`belongs_to` does not ensure reference consistency, so depending on the use case, you might also need to add a database-level foreign key constraint on the reference column, like this:
`belongs_to` does not ensure reference consistency if `optional` is set to true, so depending on the use case, you might also need to add a database-level foreign key constraint on the reference column, like this:

```ruby
create_table :books do |t|
Expand Down

0 comments on commit c13856e

Please sign in to comment.