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

allow_nil not working? #15

Closed
nozpheratu opened this issue Sep 28, 2012 · 5 comments
Closed

allow_nil not working? #15

nozpheratu opened this issue Sep 28, 2012 · 5 comments

Comments

@nozpheratu
Copy link

I might be misunderstanding it's usage, but given the following code:

 validates :state, :existence => { :allow_nil => true}

Should the parent existence only be checked if a value is provided? In other words, if it's nil no validation should take place correct?

For some reason when I add the allow_nil option block it does indeed allow nil, but it also allows me to enter any erroneous value without any existence validations kicking in.

@alotela
Copy link

alotela commented Sep 29, 2012

seems it doesn't work also when I use both and allow_nil at the same time

@AviDuda
Copy link

AviDuda commented Dec 17, 2012

+1

1 similar comment
@ghost
Copy link

ghost commented Dec 19, 2012

+1

@jmccaffrey
Copy link

I came across this from an issue on SO
http://stackoverflow.com/questions/15884024/ror-validates-existence-of-with-nil-foreign-key-not-working/15886258#15886258

I cloned the project, wrote a simple test to verify that it wouldn't save if the value was bogus, and verified that as soon as you add allow_nil => true, it will always save....NO MATTER WHAT. I debugged a bit and verified that the code is not even called at all if you set allow_nil => true. I'm not sure why that is, probably something with how the validators are registered and called in a higher-level spot, but there is a work around that I was able to use.

If I used this, my test would behave properly (invoke the logic if the value was not nil, skip it otherwise), and the record would not save if there is a non-nil, but not valid value:

validates_existence_of :name, :unless => Proc.new { |user| user.name_id.blank? }

@sankaranarayanan
Copy link
Contributor

use validates :state, :existence => true, :allow_nil => true
check this line https://github.com/perfectline/validates_existence/blob/master/test/rails31/models/user_with_allow_nil.rb#L7

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

No branches or pull requests

5 participants