stuck on a validates_presence_of unless issue

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

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

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