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 -- Posted via http://www.ruby-forum.com/.
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... > > Thanks > JoshNobody knows this? -- Posted via http://www.ruby-forum.com/.
Hi Josh, On Tue, 2009-10-13 at 19:50 +0200, Joshua Muheim wrote:> 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... > > > > Thanks > > Josh > > 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 HTH, Bill
OK, thank you. I don''t really get why Ruby handles protected this way, but at least I know how, now. :-) -- Posted via http://www.ruby-forum.com/.
On Tue, Oct 13, 2009 at 12:02 PM, Joshua Muheim < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, Oct 13, 2009 at 12:50 PM, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.rbCan''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 Donald http://destiney.com/