Wiebe Cazemier
2006-Apr-08 18:43 UTC
[Rails] Calling validates_inclusion_of out of default namespace
Hi, I''m trying to run the "validates_inclusion_of" method in a before_save hook, because the range is dependant on the related data. But I can''t figure out how to call it. When calling it normally, it says it can''t find it. I''ve tried several combinations like "ActiveRecord::Validations.validates_inclusion_of", but I can''t seem to find the logic in it. So, how do I call it? This question could be generalised, because I never know what to call when something is not in de default namespace. For example, how do I know that "ActiveRecord::ConnectionAdapters::SQLServerAdapter.select_one" can be run by "ActiveRecord::Base.connection.select_one"? I happen to know this one, but I would have never guessed... Thanks in advance.
Jeff Coleman
2006-Apr-08 19:41 UTC
[Rails] Re: Calling validates_inclusion_of out of default namespace
Wiebe Cazemier wrote:> Hi, > > I''m trying to run the "validates_inclusion_of" method in a before_save > hook, > because the range is dependant on the related data. But I can''t figure > out how > to call it. When calling it normally, it says it can''t find it.This may or may not answer your question, but you can add an ":on => :save" option to the validates_inclusion_of method. If I understand it correctly, the actual "validates_inclusion_of" method sets up filters, it''s not an actual procedure. It''s more like a configuration option than a method you''d want to run from inside another method. Jeff -- Posted via http://www.ruby-forum.com/.
Wiebe Cazemier
2006-Apr-08 21:34 UTC
[Rails] Re: Calling validates_inclusion_of out of default namespace
On Saturday 08 April 2006 21:41, Jeff Coleman wrote:> This may or may not answer your question, but you can add an ":on => > :save" option to the validates_inclusion_of method. > > If I understand it correctly, the actual "validates_inclusion_of" method > sets up filters, it''s not an actual procedure. It''s more like a > configuration option than a method you''d want to run from inside another > method.You understand it correctly. But, the validates_inclusion_of method is run when the object is created. When you do, all the attributes and relations don''t exist yet. But I do need to access it''s attributes and relations to acquire the boundries I have to give to the validates_inclusion_of method. Therefore, that method must be run _after_ the attributes and relations have been added to the object. For example, if max_score is an attribute of the object, saying validates_inclusion_of(:score, :in => 0..max_score) will say that max_score is unkown.
BigSmoke
2006-Apr-11 10:47 UTC
[Rails] Re: Calling validates_inclusion_of out of default namespace
On 4/8/06, Wiebe Cazemier <halfgaar@gmx.net> wrote:> > On Saturday 08 April 2006 21:41, Jeff Coleman wrote: > > This may or may not answer your question, but you can add an ":on => > > :save" option to the validates_inclusion_of method. > > > > If I understand it correctly, the actual "validates_inclusion_of" method > > sets up filters, it''s not an actual procedure. It''s more like a > > configuration option than a method you''d want to run from inside another > > method. > > You understand it correctly. > > But, the validates_inclusion_of method is run when the object is created. > When > you do, all the attributes and relations don''t exist yet. But I do need to > access it''s attributes and relations to acquire the boundries I have to > give > to the validates_inclusion_of method. Therefore, that method must be run > _after_ the attributes and relations have been added to the object. > > For example, if max_score is an attribute of the object, saying > > validates_inclusion_of(:score, :in => 0..max_score) > > will say that max_score is unkown.I''ve looked at your code and you tried to define the validation from a before_save hook, which is fine if you do it correctly ;-) However, you call validates_inclusion_of as an instance method instead of a class function. If you replace your call to validates_inclusion_of from the before_save hook method with self.class.validates_inclusion_of, your problem is solved. - Rowan -- Morality is usually taught by the immoral. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060411/c1dbdd5d/attachment.html
Wiebe Cazemier
2006-Apr-11 16:03 UTC
[Rails] Re: Re: Calling validates_inclusion_of out of default namespace
On Tuesday 11 April 2006 12:47, BigSmoke wrote:> I''ve looked at your code and you tried to define the validation from a > before_save hook, which is fine if you do it correctly ;-) > > However, you call validates_inclusion_of as an instance method instead of a > class function. If you replace your call to validates_inclusion_of from the > before_save hook method with self.class.validates_inclusion_of, your problem > is solved.At first glance, it seemed to work, but calling the validates method on the class itself has the unfortunate side effect that it starts acting like Singletons (as we discussed in private, ie, not here on the list), meaning that all objects get the validations of the last call to class.validates_inclusion_of. I''m getting confused here. The belongs_to* and validates_* and the likes are class methods. But when calling one of them in any ActiveRecord::Base object, aren''t they called when the Base object has already been instantiated? If so, then why can''t I call them in methods of my models?