-
Notifications
You must be signed in to change notification settings - Fork 22k
Require belongs_to
by default.
#18937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -833,6 +833,7 @@ The `belongs_to` association supports these options: | |
* `:polymorphic` | ||
* `:touch` | ||
* `:validate` | ||
* `:optional` | ||
|
||
##### `:autosave` | ||
|
||
|
@@ -956,6 +957,10 @@ end | |
|
||
If you set the `:validate` option to `true`, then associated objects will be validated whenever you save this object. By default, this is `false`: associated objects will not be validated when this object is saved. | ||
|
||
##### `:optional` | ||
|
||
If you set the `:optional` option to `true`, then associated object will be validated for presence. By default, this is `false`: associated objects will be validated for presence. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: "then associated object will NOT be validated for presence" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: By default, this is false: associated objects will be validated for presence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm talking about the first part. Look more closely, the sentence says the same thing twice:
I took the original sentence and just modified the alignement so it's visually clear they are the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, this was fixed in 803ef74 |
||
|
||
#### Scopes for `belongs_to` | ||
|
||
There may be times when you wish to customize the query used by `belongs_to`. Such customizations can be achieved via a scope block. For example: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
# Require `belongs_to` associations by default. | ||
Rails.application.config.active_record.belongs_to_required_by_default = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'll be odd to switch to
optional:
on belongs_to but keeprequired:
for has_one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. belongs_to to me strongly implies a owner/ownee relationship. Doesn't make sense in most cases to say I'm an ownee without a owner. But the other case does make sense. That the owner may or may not have an ownee. (Excuse the made-up word).