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

Clear up confusion w/ presence around belongs_to associations #1214

Merged
merged 2 commits into from
Jun 8, 2019

Conversation

mcmire
Copy link
Collaborator

@mcmire mcmire commented Jun 1, 2019

Now that belongs_to associations add a presence validation
automatically, if you have a test such as the following:

class StudentBook < ApplicationRecord
  belongs_to :student
end

RSpec.describe StudentBook do
  it { is_expected.not_to be_valid }
  it { is_expected.to validate_presence_of(:student) }
end

then the test for presence of :student will fail, because the validation
message on the automatic presence validation is different than the usual
message. The solution here, of course, is to just use belong_to, but
that is not very obvious. So this commit updates the presence matcher to
remind users about using belong_to in a case such as this.


Fixes #1095.

@mcmire mcmire force-pushed the add-extra-wording-to-presence-for-belongs_to branch from 5c49d22 to 6ef03e9 Compare June 1, 2019 23:17
Now that `belongs_to` associations add a presence validation
automatically, if you have a test such as the following:

    class StudentBook < ApplicationRecord
      belongs_to :student
    end

    RSpec.describe StudentBook do
      it { is_expected.not_to be_valid }
      it { is_expected.to validate_presence_of(:student) }
    end

then the test for presence of :student will fail, because the validation
message on the automatic presence validation is different than the usual
message. The solution here, of course, is to just use `belong_to`, but
that is not very obvious. So this commit updates the presence matcher to
remind users about using `belong_to` in a case such as this.
@mcmire mcmire force-pushed the add-extra-wording-to-presence-for-belongs_to branch from 6ef03e9 to 7cad5fe Compare June 1, 2019 23:43
@mcmire mcmire closed this Jun 8, 2019
@mcmire mcmire deleted the add-extra-wording-to-presence-for-belongs_to branch June 8, 2019 02:08
@mcmire mcmire merged commit 7cad5fe into master Jun 8, 2019
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.

validate_presence_of with belongs_to in Rails 5
1 participant