Forum: Ruby on Rails stuck on a validates_presence_of unless issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A8be277b450f30e5ea8d5278b758d48b?d=identicon&s=25 hoenth (Guest)
on 2008-11-01 14:30
(Received via mailing list)
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
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-11-01 15:06
(Received via mailing list)
On Sat, Nov 1, 2008 at 5:29 AM, hoenth <hoenth@yahoo.com> 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
A8be277b450f30e5ea8d5278b758d48b?d=identicon&s=25 hoenth (Guest)
on 2008-11-03 04:50
(Received via mailing list)
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
This topic is locked and can not be replied to.