Jim Burgess wrote:
> Hi,
> 
> If I want to ensure that someone has filled out the email section of a
> form I can write this in my model:
> 
> validates_presence_of :email
> 
> I can also achieve (more or less) the same thing by writing:
> 
> validate do |applicant|
>   applicant.validate_presence("email")
> end
> 
> def validate_presence(arg)
>   string = "errors.add(:#{arg}, \"can't be blank\") if #{arg} == \"\""
>   eval(string)
> end

Here's the eval-less version:

def validate_presence(arg)
  errors.add arg.to_sym, "can't be blank" if self.send(arg).blank?
end

Of course, since this is ActiveRecord, you could use self[arg] instead 
of self.send(arg) in most cases, but it's not quite equivalent. (I also 
took the liberty of using blank? .)

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to