I'm assuming you don't want to create a Person if your User
validations fail and vice versa. One way to do it is as follows. This
will validate your Person during the validation phase for User, and if
all is good save your Person then save User. Note that this doesn't
preclude any database constraint/error from screwing up your User
save, resulting in orphaned Persons. To be really safe you should wrap
both Person and User save operations in a transaction (http://
api.rubyonrails.org/classes/ActiveRecord/Transactions/
ClassMethods.html).
class User < ActiveRecord::Base
bleongs_to :person, :validate => true
before_validation :create_person
before_create :save_person
def create_person
if person.nil? && person_id.nil?
self.person = Person.new(:name => email, :tenant => tenant)
end
end
def save_person
self.person.save!
end
end