Forum: Ruby on Rails What are they, kind of method invoking ?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A5e4a7e319e30d7cbaad72eac7268a75?d=identicon&s=25 (Guest)
on 2007-07-09 09:43
(Received via mailing list)
We do(declare?) fields validations like this:

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

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 ?
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-07-09 10:11
(Received via mailing list)
On 7/9/07, <> 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
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 :)  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?")

6815ff92d29338926480464468e8288c?d=identicon&s=25 Craig McClanahan (Guest)
on 2007-07-09 10:16
(Received via mailing list)
On 7/9/07, <> 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
A5e4a7e319e30d7cbaad72eac7268a75?d=identicon&s=25 (Guest)
on 2007-07-20 16:59
(Received via mailing list)
Thank you guys, your answers are really helpfull, but I still need to
know more about Ruby to fully understand it.
A5e4a7e319e30d7cbaad72eac7268a75?d=identicon&s=25 (Guest)
on 2007-07-20 17:22
(Received via mailing list)
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
    def validate
      errors.add_on_empty(["title", "body"])
      errors.add("title", "must be at most 10 characters") if
title.size > 10

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.
This topic is locked and can not be replied to.