# Stuck on a validates\_presence\_of unless issue

**URL:** <https://www.ruby-forum.com/t/stuck-on-a-validates-presence-of-unless-issue/152294>\
**Category:** Rails\
**Created:** [November 1, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/stuck-on-a-validates-presence-of-unless-issue/152294 "2008-11-01T14:30:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoenth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/13edae/32.png) [@hoenth](https://www.ruby-forum.com/u/hoenth)\
**Post date:** [November 1, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/stuck-on-a-validates-presence-of-unless-issue/152294/1 "2008-11-01T14:30:02Z")

</div>

i have a person object. Persons don’t need to have addresses, but if  
they have any address field value, they must have them all. So I have  
something like this:

validates\_presence\_of :street\_address, :city, :state, :postal\_code  
unless :address\_blank?

address\_blank? checks whether all of the address fields are blank.

If I run a test like this, it works:

describe “given attributes” do  
before(:each) do  
@valid\_attributes = {  
:first\_name =\> “Jonny”,  
:middle\_name =\> “D”,  
:last\_name =\> “Miller”,  
:street\_address =\> “123 This Street”,  
:street\_address2 =\> “Suite 200”,  
:city =\> “Baltimore”,  
:state =\> “MD”,  
:postal\_code =\> “23993”,  
:salutation =\> “Mr.”,  
:home\_phone =\> “373-333-9999”,  
:cell\_phone =\> “373-444-5555”  
}  
end  
it “given attributes containing valid address values,  
address\_blank? should return false” do  
it = Person.new(@valid\_attributes)  
it.address\_blank?.should == false  
end

However, if I do the following,

it “must have a street\_address” do  
@valid\_attributes[:street\_address] = “”  
it = Person.new(@valid\_attributes)  
it.save  
it.errors.on(:street\_address).should == “can’t be blank”  
end

I get the following result.

‘Person must have a street\_address’ FAILED  
expected: “can’t be blank”,  
got: nil (using ==)

So it appears that :address\_blank? in the validates\_presence\_of is  
evaluating to true.

Can anyone give me an idea of how I can detect why this is so?

I apologize in advance if this should be an rspec question, and will  
take it there if advised as such.

best,  
Tom

---

<div class="post-metadata">

**Author:** ![hoenth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/13edae/32.png) [@hoenth](https://www.ruby-forum.com/u/hoenth)\
**Post date:** [November 1, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/stuck-on-a-validates-presence-of-unless-issue/152294/2 "2008-11-01T15:06:19Z")

</div>

On Sat, Nov 1, 2008 at 5:29 AM, hoenth [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i have a person object. Persons don’t need to have addresses, but if  
> they have any address field value, they must have them all. So I have  
> something like this:
> 
> validates\_presence\_of :street\_address, :city, :state, :postal\_code  
> unless :address\_blank?

Instead of

unless :address\_blank?

use

:unless =\> :address\_blank?

since you need to pass options to validates\_presence\_of as opposed to  
writing a normal conditional. See the API docs for validates\_presence\_of  
for  
more details if you need them.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![hoenth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/13edae/32.png) [@hoenth](https://www.ruby-forum.com/u/hoenth)\
**Post date:** [November 3, 2008, 4:50am UTC](https://www.ruby-forum.com/t/stuck-on-a-validates-presence-of-unless-issue/152294/3 "2008-11-03T04:50:56Z")

</div>

I must have looked at those API docs 10 times and didn’t see it. Would  
have bitten…and all that.

Thanks for the quick response Craig. Worked like a charm.  
Tom
