Dave Castellano
2013-Mar-10 14:14 UTC
How to create an object in one model at the same time as another
Hi, I''m using Devise and Cancan in my project. I''m a beginner still. I''m trying to create a new Profile object each time a new user is created. Anyone know where to do this, as there is no new action in User controller. I have a user model and a Profile model User model: user has_one :profile Profile model: profile belongs_to :user How to create an object in one model at the same time as another -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jodi Showers
2013-Mar-10 16:16 UTC
Re: How to create an object in one model at the same time as another
use a model callback -
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
:after_create :create_profile
private
def create_profile
...
end
On Sun, Mar 10, 2013 at 10:14 AM, Dave Castellano
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:
> Hi,
> I''m using Devise and Cancan in my project. I''m a
beginner still. I''m
> trying to create a new Profile object each time a new user is created.
> Anyone know where to do this, as there is no new action in User
> controller.
>
> I have a user model and a Profile model
>
> User model:
> user has_one :profile
>
> Profile model:
> profile belongs_to :user
>
> How to create an object in one model at the same time as another
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Mar-10 17:00 UTC
Re: How to create an object in one model at the same time as another
On 10 March 2013 14:14, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I''m using Devise and Cancan in my project. I''m a beginner still. I''m > trying to create a new Profile object each time a new user is created. > Anyone know where to do this, as there is no new action in User > controller. > > I have a user model and a Profile model > > User model: > user has_one :profile > > Profile model: > profile belongs_to :user > > How to create an object in one model at the same time as anotherHave a look at the Rails Guide on ActiveRecord Associations and you will see that for a has_one association there will be a create_association method defined for you. So in the create action for a user, after it is successfully saved you can call create_profile to create and save a new profile. Alternatively, as Jodi has suggested, you could put this in an after_create callback. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Dave Castellano
2013-Mar-10 21:38 UTC
Re: How to create an object in one model at the same time as another
after it is successfully saved you can call create_profile> to create and save a new profile. Alternatively, as Jodi has > suggested, you could put this in an after_create callback. > > ColinThanks. The after_create callback works nicely as I have no new or create actions in the users controller using Devise/Cancan. Not sure where they but thats another weekend project! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.