What are they, kind of method invoking?

We do(declare?) fields validations like this:

class Dream < ActiveRecord::Base
validates_presence_of :title, :description
validates_length_of :description, :minimum => 10
end

I know how to use them, and have already used them in my applications,
but I’m curious about some syntax problem.

I’m new to Ruby and Rails, It may be more a Ruby question than Rails’.
My question is:
What are those lines of code ?
validates_presence_of :title, :description
validates_length_of :description, :minimum => 10
Declaration ? Method calling ?

If they’re declaration, how could they come with some parameters ?
Looks like they’re callings, but how could they exist here in a class
variable definition area , who are calling them and when are they
being called ?

On 7/9/07, [email protected] [email protected] wrote:

but I’m curious about some syntax problem.
variable definition area , who are calling them and when are they
being called ?

Believe it or not, those are method calls.

The first thing to remember in Ruby is that a method call does not
require parentheses, so the following two lines are equivalent:

validates_presence_of :title, :description
validates_presence_of(:title, :description)

The second thing to remember is that Ruby classes are “open” … that
is, at run time, you can add new method definitions, or change
existing ones … and that’s the sort of thing that goes on in a lot
of the magic that Rails supports. This syntactical quirk of Ruby,
plus a pretty elegant design of helper methods in Rails, gives you a
feel that you are using a “domain specific language” to specify the
characteristics of your model objects, even though … deep down …
it’s just a bunch of Ruby methods provided by Rails.

This doesn’t quite cover all the magic that is going on, however …
you’ll notice that your ActiveRecord classes can deal with method
calls like name() or name=() if the underlying table has a “name”
column. These capabilities are not actually provided by specific
methods … to understand the nitty gritty details, you’ll have to
become familiar with Ruby concepts like the method_missing() method.
But it’s all open source, and there’s lots of people willing to help
you figure that stuff out if you want to.

In the mean time, it’s sure fun (coming from a Java background like I
do) to not have to write getter and setter methods, or any of that
other stuff that Java frameworks require :-).

Craig McClanahan

On 7/9/07, [email protected] [email protected] wrote:

but I’m curious about some syntax problem.
variable definition area , who are calling them and when are they
being called ?

The validates_blah_of methods are class methods. They’re a part of
the ActiveRecord::Validations::ClassMethods module which gets mixed
into ActiveRecord::Base. Check out
http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html
for a bit more info.

They could have been written validates_presence_of(:title,
:description) as well, but Ruby lets you omit parentheses. In this
case, omitting the parens makes the code look prettier :slight_smile: I guess it
makes it look more declarative or built in rather than being a call.

I’d suggest reading the Pickaxe to fully understand the difference
between class and instance methods (which will answer your question
“who’s calling them?”)

Pat

Thank you guys, your answers are really helpfull, but I still need to
know more about Ruby to fully understand it.

I found the pre 0.9 way of doing this kind of validations pretty much
answered my question.
(pre 0.9):

An example: (validating something I’m not sure.)

class Post < ActiveRecord::Base
protected
def validate
errors.add_on_empty([“title”, “body”])
errors.add(“title”, “must be at most 10 characters”) if
title.size > 10
end
end

You see, It is justing adding some error messages to a container.
and :
validates_presence_of :title, :description
validates_length_of :description, :minimum => 10
as I understand, nearly do the same thing but in Ruby way that is :
calling a class method whenever a calss instance is created.