You might want to use after_initialize callback, e.g.

  after_initialize :build_person_if_no_person

  def build_person_if_no_person
    build_person if new_record? && person.nil?
  end

The person.nil? check might be needed in some cases, for example if
you want to use Person.new.build_user.

(I did not try the above code, but this is almost an excerpt from my
application, which uses this approach).

I wouldn't recommend creating object in before_validation callback,
because:
1) when validation fails, you're left with not needed Person records
in the database
2) one day you may want to save User and skip validation, in which
case you won't have the Person object created.

Overall, I like the idea of using composition between User and Person
instead of inheritance - I usually do it the same way in my
applications.

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to