# Symbol pointing to a method idiom in ror model

**URL:** <https://www.ruby-forum.com/t/symbol-pointing-to-a-method-idiom-in-ror-model/182095>\
**Category:** Rails\
**Created:** [January 22, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/symbol-pointing-to-a-method-idiom-in-ror-model/182095 "2010-01-22T16:28:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@chisa](https://www.ruby-forum.com/u/chisa)\
**Post date:** [January 22, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/symbol-pointing-to-a-method-idiom-in-ror-model/182095/1 "2010-01-22T16:28:41Z")

</div>

IN NEED OF EXPLANATION:

class Comment \< ActiveRecord::Base  
validate :must\_be\_friends

```
def must_be_friends
  errors.add_to_base("Must be friends to leave a comment")
             unless commenter.friend_of?(commentee)
end

```

end

The above code comes from rails documentation. There is a similar  
example inAgile Web D. with Rails by Dave T. and Sam Ruby.

my understanding is that:  
validate :must\_be\_friends  
is a method call to the validate method within the ActiveRecord::Base  
class. Supposedly it’s contents are:  
def validate  
end

This of course does not work with ruby because the validate method does  
not take parameters.

Is metaporgramming being used? Tnis seems like some sort of Rails  
idiom.

Please explain.

I’m trying to get back into Rails after my first attempt.

TIA,  
Pete

---

<div class="post-metadata">

**Author:** ![chisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@chisa](https://www.ruby-forum.com/u/chisa)\
**Post date:** [January 25, 2010, 12:42am UTC](https://www.ruby-forum.com/t/symbol-pointing-to-a-method-idiom-in-ror-model/182095/2 "2010-01-25T00:42:46Z")

</div>

It’s a stub of sorts. When you use “validate :some\_symbol” in your  
model, it’s basically adding that symbol to a stack that it’s going to  
invoke “model\_instance.send(:some\_symbol)” (more or less) on when  
validation happens.

---

<div class="post-metadata">

**Author:** ![chisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@chisa](https://www.ruby-forum.com/u/chisa)\
**Post date:** [January 25, 2010, 9:28am UTC](https://www.ruby-forum.com/t/symbol-pointing-to-a-method-idiom-in-ror-model/182095/3 "2010-01-25T09:28:53Z")

</div>

On Jan 22, 3:28 pm, Hiro P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This of course does not work with ruby because the validate method does  
> not take parameters.
> 
> Is metaporgramming being used? Tnis seems like some sort of Rails  
> idiom.

Not really: there are two validate methods. One is an instance method  
that takes no arguments, the other is a class method that takes a  
method name or block and adds it to the list of validations to run  
(this is slightly hidden by the fact that that method comes from the  
ActiveSupport callback stuff)

Fred
