Why need before_validation etc methods be public?

Joshua Muheim wrote:

Hi all

I just wondered why a private before_validation method is not called
when an ActiveRecord object is saved? When it's public, then it works...
Why is that? In my opinion, the before_validation method should
intuitively rather be private...


Nobody knows this?

Hi Josh,

OK, thank you. I don't really get why Ruby handles protected this way,
but at least I know how, now. :slight_smile:

OK, thank you. I don’t really get why Ruby handles protected this way,

but at least I know how, now. :slight_smile:

Most languages which has this protected keyword handles it this way. Another option would be

to do something like this:

class SomeModel < ActiveRecord::Base

before_validation :some_method


def some_method



Now, the callback is properly registered and the method, some_method, can be

properly invoked at the appropriate time.

Good luck,


Nobody knows this?

No.. it's just that no one cares :slight_smile:

Ruby's private and protected members aren't very private or protected.
It's more of an "intent" thing than a hard rule.

#!/usr/bin/env ruby

class Foo
  def hidden
    puts "I am hidden"

f = Foo.new

  puts "Can't run hidden: #{ $! }"

f.send( :hidden )


Can't run hidden: private method `hidden' called for #<Foo:0xb7c2f92c>
I am hidden

What's happening to you is the filter execution only sees private
methods. It's looking for the method name, doesn't find it, and
doesn't run it. Look to see how it appears and disappears from the
private methods list when you move it around.

u = User.find(:first)

OK, strange. Anyway, thanks to you guys!