-
-
Notifications
You must be signed in to change notification settings - Fork 909
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
Rails 5: belongs_to :class, optional: true #870
Comments
Okay -- do you plan on using |
Yes. I'm developing a app where a Amiibo may or may not to belongs to Series. ex.: Sonic amiibo belongs to Smash Bros. Series, but Wolf Link amiibo belongs to none. |
Cool. I'm not sure when I'll be able to get to this, as I am working on resolving some regressions with the 3.0 release right now, but I would happily take a PR, otherwise I'll get to it at some point down the line. I'll leave this open in the meantime since it's related to Rails 5. |
I have a use-case for this as well. |
I also have a use case for this. A food database where most of the foods are not created by users, but users can create foods and 'own' the foods that they create. Workaround: the admin user profile will own foods by default. Please correct me if there's a better workaround or existing solution. |
You can use |
@stephanngamedev I did neither of those things and it seems like I can create foods with user_id: nil no problem--am I missing something? My migration:
|
Are you using Rails 5? |
I am using Rails 4. Sorry, I see now this thread is labeled Rails 5. Would your recommendations be necessary in Rails 5? |
Yes. My recomendations is to make rails 5 belongs_to behavior like rails 4 belongs_to |
@leoebrown Rails 5 changed the behavior of |
Hi! there is some progress on this? I used to have According to the new Rails behaviour, do you think that shoulda-matchers should test this by assigning nil and checking if and error was added? And if a qualifier was passed, the inverse check.. |
@vizcay for what it's worth, you can get the spec to pass by adding
|
Given a Rails 5 model with the association,
I should be able to test the association with:
to verify that the presence of an associated model is not required. |
@ybakos, I tested it doesn't work on |
We are only testing for validation of the presence of the association ID, what if the given ID is invalid. And app behaviour is not to validate the existence of such association? |
@mcmire @stephanngamedev Isn't that issue resolved by now? |
Doesn't seem to work in version 3.1.2 for I am getting: |
Not sure why this issue wasn't closed automatically, but yes, this is resolved by #1058. It'll be available in the next version, or you're feeling adventurous, you can point to the master branch. |
@mcmire When will this version be released? |
Any news on this? Thanks :) |
Failure/Error: it{is_expected.to belong_to(:position).optional true}
NoMethodError:
undefined method `optional' for #<Shoulda::Matchers::ActiveRecord::AssociationMatcher:0x00000000024649c8>
Did you mean? options |
@namtx @akhilesh05 @lucascaton We have a pre-release version out now! Try v4.0.0.rc1 to get |
In v4.0.0+, the usage is:
without the bool argument. |
We previously updated the shoulda-matchers gem, and now it is necessary to specify in the spec if the relationship is optional More info: thoughtbot/shoulda-matchers#870 (comment) Also the course relationship was validated as required, and at the same time as optional, so I left it as required
belongs_to
will now trigger a validation error by default if the association is not present.We can turn this off on a per-association basis with
optional: true
.rails/rails#18937
We will need a qualifier for this.
The text was updated successfully, but these errors were encountered: