# Why need before\_validation etc methods be public?

**URL:** <https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085>\
**Category:** Rails\
**Created:** [October 11, 2009, 11:15am UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085 "2009-10-11T11:15:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 11:15am UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/1 "2009-10-11T11:15:23Z")

</div>

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…

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 13, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/2 "2009-10-13T19:50:32Z")

</div>

Joshua M. 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…
> 
> Thanks  
> Josh

Nobody knows this?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 13, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/3 "2009-10-13T20:14:30Z")

</div>

Hi Josh,

On Tue, 2009-10-13 at 19:50 +0200, Joshua M. wrote:

> Nobody knows this?

Make it protected, not private. The link below should help  
understanding.

[http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby](http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby)

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 13, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/4 "2009-10-13T21:02:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 13, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/5 "2009-10-13T23:00:11Z")

</div>

On Tue, Oct 13, 2009 at 12:02 PM, Joshua M. \<  
removed\_email\_address@domain.invalid\> wrote:

> OK, thank you. I don’t really get why Ruby handles protected this way,  
> but at least I know how, now. 🙂

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

private

```
def some_method

end

```

end

Now, the callback is properly registered and the method, some\_method,  
can  
be  
properly invoked at the appropriate time.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 14, 2009, 2:05am UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/6 "2009-10-14T02:05:47Z")

</div>

On Tue, Oct 13, 2009 at 12:50 PM, Joshua M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Nobody knows this?

No… it’s just that no one cares 🙂

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  
private  
def hidden  
puts “I am hidden”  
end  
end

f = Foo.new

begin  
f.hidden  
rescue  
puts “Can’t run hidden: #{ $! }”  
end

f.send( :hidden )

> ./private.rb  
> 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)  
u.private\_methods

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 14, 2009, 10:22am UTC](https://www.ruby-forum.com/t/why-need-before-validation-etc-methods-be-public/177085/7 "2009-10-14T10:22:05Z")

</div>

OK, strange. Anyway, thanks to you guys!
