Skip to content

checking attributes in :if

August 18, 2011

so i recently wanted to write a validation like this:

validates_format_of :zipcode, :with => /\w{3}\s\w{3}/, :message => "Canadian postal code should be of the format A0A 0A0", :if => self.country.iso == "CA"

Turns out i need to pass it through a proc:

validates_format_of :zipcode, :with => /\w{3}\s\w{3}/, :message => "Canadian postal code should be of the format A0A 0A0", :if => Proc.new{self.country.iso == "CA"}

It is probably because when it is getting analyzed, the self is really the class and not the object.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: