Overloading #initialize in a model

Nick
Sat, 01 Nov 2008 16:13:13 -0000

I''m using WhinyProtectedAttributes:
http://henrik.nyh.se/2007/10/whiny-protected-attributes

Say I have this model:

class Person < ActiveRecord::Base
  attribute_accessible :name
end

If I then create a Person with an invalid attribute, an exception is
raised. For example:
  Person.new :name => ''Bob'', :asdf =>
''asdf''

What I''d like to do is, in the Person model, modify the hash of
attributes before they''re set. This is what I came up with:

class Person < ActiveRecord::Base
  attribute_accessible :name

  def initialize(attribs = {})
    attribs_that_exist = AccessibleModelAttributes.extract self.class,
attribs
    super attribs_that_exist
  end
end

Am I on the right track? Cheers,
Nick
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---