Skip to content
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

Add optional(false) support to belong_to #1237

Conversation

guialbuk
Copy link
Collaborator

Allows belongs_to associations with optional: false

belongs_to :copany, optional: false

to be tested with

it { should belong_to(:copany).optional(false) }

It also adds support for optional(true) which is the same as just optional.

Fixes #1219

@guialbuk guialbuk added this to the v4.2.0 milestone Aug 20, 2019
@mcmire
Copy link
Collaborator

mcmire commented Aug 21, 2019

Oh nice! I just did a quick pass of this and it looks good but I will take a closer look soon.

@bbugh
Copy link

bbugh commented Aug 23, 2019

Hurray! 🎉 Our app has belongs_to_required_by_default = false and only sets optional: false when required, which means there's no way for us currently to test this with shoulda-matchers. We need to explicitly test optional(false). Thanks for doing this PR!

Copy link
Collaborator

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, looks good! Merge at will 👍

@guialbuk guialbuk marked this pull request as ready for review September 4, 2019 05:03
@guialbuk guialbuk merged commit ed962de into thoughtbot:master Sep 4, 2019
@guialbuk guialbuk deleted the add-option-to-belong-to-optional-qualifier branch September 4, 2019 05:05
@XavierMeunier
Copy link

Thanks for your PR
I'm also waiting for this feature to be released, is there any planned date for the release of 4.2 ?

This will also be usefull for conditional optional in Rails 5:
belongs_to :category, optional: -> { draft? }

@guialbuk guialbuk mentioned this pull request Dec 26, 2019
guialbuk added a commit that referenced this pull request Jan 9, 2020
### Features

* Add support for `optional: false` in `belongs_to` associations. ([#1237])
* Add support for associations with `inverse_of: false` and non-standard foreign key ([#1106])

### Bug fixes
* Fix typos in documentation of `allow_values` ([#1241])
* Fix appraisal command in CONTRIBUTING.md file ([#1253])

### Improvements
* Remove minitest reporters dependency ([#1251])
* Development dependency updates

[#1106]: #1106
[#1237]: #1237
[#1241]: #1241
[#1253]: #1253
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

belong_to does not support optional(false)
4 participants