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

validate_length_of does not work when used with associations #1007

Closed
LukasBarry opened this issue Apr 7, 2017 · 8 comments · Fixed by #1569
Closed

validate_length_of does not work when used with associations #1007

LukasBarry opened this issue Apr 7, 2017 · 8 comments · Fixed by #1569

Comments

@LukasBarry
Copy link

I have been attempting to write a test on a validation for length, and I keep getting the same error. I created a small sample app, as simple as possible, to show the error: Sample Project

Basically, I have a has_many association that must have at least 1, but when I run the test
should validate_length_of(:books).is_at_least(1) I get the following error:

NoMethodError:
       undefined method `each' for "":String

My sample project current models:

class Author < ApplicationRecord
  has_many :books

  validates :books, length: { minimum: 1 }
end
class Book < ApplicationRecord
  belongs_to :author, dependent: :destroy
end

And my current test file:

require 'rails_helper'

describe Author do
  it 'validates length of books' do
    should validate_length_of(:books).is_at_least(1)
  end
end

I believe this is because Shoulda is using a string to test length, but I am attempting to test the length of a collection and not a string. However, while attempting to trace it back through the gem, I found nothing that I could use to fix my issue.

Any help would be greatly appreciated. Thanks!

@mcmire
Copy link
Collaborator

mcmire commented Apr 7, 2017

Hey @LukasBarry. When using is_at_least, the matcher will build a string with length of <length> - 1 (which in this case is 0), set the attribute (books) to that string, and assert that this causes a failure. You can see that happening here: https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/validate_length_of_matcher.rb#L340

Regardless of how you are using validate_length_of, however, the matcher does make the assumption that the column you're testing is a string column; it's not designed to work with anything else. I've never used the validation with associations before, but after reading the Rails source code, it does seem like it's a valid use case, so this is new to me. We'll need a PR to fix this. Can you submit one?

@LukasBarry
Copy link
Author

Absolutely, I will get one together and submit it. Thanks!

@mcmire mcmire changed the title testing validate_length_of on collection is failing validate_length_of does not work when used with associations Jul 25, 2017
@ysyyork
Copy link

ysyyork commented Nov 14, 2017

Hi, I just came across the same issue and was planning to make changes. But have you already started the change? And is there any update?

@mcmire
Copy link
Collaborator

mcmire commented Nov 15, 2017

Hey @ysyyork! As you can see here there is a PR here that was submitted. While it's a good start it does need some tests. Unfortunately this issue is low on our priority list at the moment but if you would like to pick it up and supply the tests (submitting a new PR of course) then that would be much appreciated!

@ysyyork
Copy link

ysyyork commented Nov 15, 2017

@mcmire Sure, I will give it a look if I have the time.

@prashantjois
Copy link

Hi all, looks like this issue never got resolved. I have a fresh PR open: #1124

@mcmire
Copy link
Collaborator

mcmire commented May 6, 2020

Hey folks. In an effort to lighten our load as maintainers and be able to serve you better in the future, the shoulda-matchers team is working on cleaning out the cobwebs in this repo by pruning the backlog. As there are few of us, there are a lot of items that will simply never earn our attention in a reasonable time frame, and rather than giving you an empty promise, we think it makes more sense to focus on more recent issues. That means, unfortunately, that we must close this issue.

Don't take this the wrong way: our aim is not to diminish the effort people have made or dismiss problems that have been raised. If you feel that we should reopen this issue, then please let us know so that we can reprioritize it. Thanks!

@mcmire mcmire closed this as completed May 6, 2020
@mcmire
Copy link
Collaborator

mcmire commented Jul 11, 2020

Reopening since this is dependent on a PR.

@mcmire mcmire reopened this Jul 11, 2020
matsales28 added a commit to matsales28/shoulda-matchers that referenced this issue Aug 11, 2023
This commit allows the length matcher to be used on associations. It
does this by checking if the attribute is an association, and if so, it
uses the associations as the attribute to validate.

This commit also test for the length matcher on associations (has_many and has_many through).

I want to give credit to @prashantjois for the initial work on this
feature. I took his work and expanded on it.

Closes thoughtbot#1007
matsales28 added a commit to matsales28/shoulda-matchers that referenced this issue Aug 11, 2023
This commit allows the length matcher to be used on associations. It
does this by checking if the attribute is an association, and if so, it
uses the associations as the attribute to validate.

This commit also test for the length matcher on associations (has_many and has_many through).

I want to give credit to @prashantjois for the initial work on this
feature. I took his work and expanded on it.

Closes thoughtbot#1007
matsales28 added a commit to matsales28/shoulda-matchers that referenced this issue Aug 15, 2023
This commit allows the length matcher to be used on associations. It
does this by checking if the attribute is an association, and if so, it
uses the associations as the attribute to validate.

This commit also test for the length matcher on associations (has_many and has_many through).

I want to give credit to @prashantjois for the initial work on this
feature. I took his work and expanded on it.

Closes thoughtbot#1007
matsales28 added a commit that referenced this issue Aug 18, 2023
This commit allows the length matcher to be used on associations. It
does this by checking if the attribute is an association, and if so, it
uses the associations as the attribute to validate.

This commit also test for the length matcher on associations (has_many and has_many through).

I want to give credit to @prashantjois for the initial work on this
feature. I took his work and expanded on it.

Closes #1007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants