2011/8/28 J. Pablo Fernández <[email protected]>

> Hello,
>
> I have two models, User and Person. Basically users are users of the system
> while people is a kind of address book. But every user is also a person so I
> created this relationship:
>
> class User < ActiveRecord::Base
>   belongs_to :person
> end
>
> class Person < ActiveRecord::Base
>   has_one :user
> end
>
> Now, on creating a user I want to automatically create a person. Is this
> the way to do it:
>
>   before_validation :create_person
>
>   def create_person
>     if person.nil? && person_id.nil?
>       self.person = Person.create!(:name => email, :tenant => tenant)
>     end
>   end
>
>
If you use the method above, there's a big chance that a Person is created
when the associated User fails the validation. Look at
accepts_nested_attributes_for.


> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/QwM_6LtsGoUJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>



-- 
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

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

Reply via email to