Forum: Ruby on Rails Introspection of validates_presence_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
169474d3cfbe3c38863ca8a826738148?d=identicon&s=25 СеÑ?гей Ð?ойко (Guest)
on 2006-05-18 12:01
(Received via mailing list)
Hello all.

I don't know how to get all symbols passed to method
validates_presence_of in model. I've trying to use following solution:

class News < ActiveRecord::Base
  validates_presence_of :title, :text

  def validates_presence_of(*attr_names)
    @@obligatory_fields = attr_names
    ActiveRecord::Base.validates_presence_of(*attr_names)
  end

  def obligatory_fields
    @@obligatory_fields
  end
end

But when object news was created i've got following error message:

uninitialized class variable @@obligatory_fields in News.

I don't understand when object News call methods:
validates_presence_of, attr_accessible, attr_reader, etc.
Maybe someone know how to deal with this problem?

--
 Serhiy Boiko, CRIS-UANIC
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-05-18 18:05
(Received via mailing list)
On 5/18/06, СеÑ?гей Ð?ойко <cris@univ.kiev.ua> wrote:
>   end
> end

Try:

class News < ActiveRecord::Base
  def self.validates_presence_of(*attr_names)
    @@obligatory_fields = attr_names
    super(*attr_names)
  end

  validates_presence_of :title, :text
  cattr_accessor :obligatory_fields
end
This topic is locked and can not be replied to.