Chris
2010-Aug-29 17:24 UTC
Automatically create one child record when parent record is initially created
Sorry for the newbie question in advance... I''ve got three models, a user, site and user_site model. A site can have many users and a user can have many sites. The relationship between these two is stored in user_site. In my model defs user and sites both have has_many user_sites statement. Within my user_sites file, there are two belongs_to statements, one for user and one for site. within the What I''m looking to do is when a user is created initially to automatically create a record in user_site for a default site (I have it contained in an instance variable within the user controller). What I''m not sure is how to most effectively do this so that the generated id for user is automatically pulled into the child record in user_site, basically I''m trying to come up with an efficient method for creating the user and user_site records. Any guidance is much appreciated. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bill Walton
2010-Aug-29 17:31 UTC
Re: Automatically create one child record when parent record is initially created
Hi Chris, On Sun, Aug 29, 2010 at 12:24 PM, Chris <cdellinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry for the newbie question in advance... I''ve got three models, a > user, site and user_site model. A site can have many users and a user > can have many sites. The relationship between these two is stored in > user_site. In my model defs user and sites both have has_many > user_sites statement. Within my user_sites file, there are two > belongs_to statements, one for user and one for site. within the What > I''m looking to do is when a user is created initially to automatically > create a record in user_site for a default site (I have it contained > in an instance variable within the user controller). What I''m not > sure is how to most effectively do this so that the generated id for > user is automatically pulled into the child record in user_site, > basically I''m trying to come up with an efficient method for creating > the user and user_site records. Any guidance is much appreciated.Use an after_create filter in your User model to create and populate the record in user_sites with the user_id. HTH, Bill -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris
2010-Aug-29 17:34 UTC
Re: Automatically create one child record when parent record is initially created
How would I handle a failure in that second piece? Can I put it in transaction and roll it back? On Aug 29, 1:31 pm, Bill Walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Chris, > > On Sun, Aug 29, 2010 at 12:24 PM, Chris <cdellin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Sorry for the newbie question in advance... I''ve got three models, a > > user, site and user_site model. A site can have many users and a user > > can have many sites. The relationship between these two is stored in > > user_site. In my model defs user and sites both have has_many > > user_sites statement. Within my user_sites file, there are two > > belongs_to statements, one for user and one for site. within the What > > I''m looking to do is when a user is created initially to automatically > > create a record in user_site for a default site (I have it contained > > in an instance variable within the user controller). What I''m not > > sure is how to most effectively do this so that the generated id for > > user is automatically pulled into the child record in user_site, > > basically I''m trying to come up with an efficient method for creating > > the user and user_site records. Any guidance is much appreciated. > > Use an after_create filter in your User model to create and populate > the record in user_sites with the user_id. > > HTH, > Bill-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bill Walton
2010-Aug-29 18:33 UTC
Re: Re: Automatically create one child record when parent record is initially created
On Sun, Aug 29, 2010 at 12:34 PM, Chris <cdellinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How would I handle a failure in that second piece? Can I put it in > transaction and roll it back?Probabl, though you''d have to throw an exception for a transaction to be effective. IIRC, Rails3 has support for creating associated records built in but I haven''t gotten around to working with 3 and you didn''t say what version you''re using. The easiest, most readable thing to do might be to just, in the controller... if @user.save and @user.user_site .... It''s probably better, though, to move it into the User model''s after_create (untested code) after_create :create_default_user_site_rec def create_default_user_site_rec user_site_rec = UserSite.new user_site_rec.user_id = self.id unless user_site.save self.destroy false end end Tricky thing here is I''m not sure off the top of my head if the failure of the after_create and its returning false will translate into the save itself returning false. Sorry I don''t have the time to run it to ground for you. Hopefully this will give you some options to investigate. Best regards, Bill end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris
2010-Aug-30 00:50 UTC
Re: Automatically create one child record when parent record is initially created
I am using Rails 3. I got it to work with the following code, but I haven''t done anything to make it transactional (a user record will get created even if the second creation fails) @user = User.new(params[:user]) if @user.save @user.user_sites << UserSite.create(:site_id => @current_site.id) On Aug 29, 2:33 pm, Bill Walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Aug 29, 2010 at 12:34 PM, Chris <cdellin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > How would I handle a failure in that second piece? Can I put it in > > transaction and roll it back? > > Probabl, though you''d have to throw an exception for a transaction to > be effective. IIRC, Rails3 has support for creating associated > records built in but I haven''t gotten around to working with 3 and you > didn''t say what version you''re using. > > The easiest, most readable thing to do might be to just, in the controller... > > if @user.save and @user.user_site > .... > > It''s probably better, though, to move it into the User model''s > after_create (untested code) > > after_create :create_default_user_site_rec > > def create_default_user_site_rec > user_site_rec = UserSite.new > user_site_rec.user_id = self.id > unless user_site.save > self.destroy > false > end > end > > Tricky thing here is I''m not sure off the top of my head if the > failure of the after_create and its returning false will translate > into the save itself returning false. Sorry I don''t have the time to > run it to ground for you. Hopefully this will give you some options > to investigate. > > Best regards, > Bill > > end-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris
2010-Aug-30 00:53 UTC
Re: Automatically create one child record when parent record is initially created
I am using Rails 3. I got it to work with the following code, but it is not currently transactional (if the creation of a UserSite fails the original user record will still exist in the db). @user = User.new(params[:user]) if @user.save @user.user_sites << UserSite.create(:site_id => @current_site.id) ... end While this works I''m not sure if there is a better way to accomplish this (some Rails goodness I''m not familiar with). If anyone has an opinion on it, please let me know. On Aug 29, 2:33 pm, Bill Walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Aug 29, 2010 at 12:34 PM, Chris <cdellin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > How would I handle a failure in that second piece? Can I put it in > > transaction and roll it back? > > Probabl, though you''d have to throw an exception for a transaction to > be effective. IIRC, Rails3 has support for creating associated > records built in but I haven''t gotten around to working with 3 and you > didn''t say what version you''re using. > > The easiest, most readable thing to do might be to just, in the controller... > > if @user.save and @user.user_site > .... > > It''s probably better, though, to move it into the User model''s > after_create (untested code) > > after_create :create_default_user_site_rec > > def create_default_user_site_rec > user_site_rec = UserSite.new > user_site_rec.user_id = self.id > unless user_site.save > self.destroy > false > end > end > > Tricky thing here is I''m not sure off the top of my head if the > failure of the after_create and its returning false will translate > into the save itself returning false. Sorry I don''t have the time to > run it to ground for you. Hopefully this will give you some options > to investigate. > > Best regards, > Bill > > end-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris
2010-Aug-30 01:30 UTC
Re: Automatically create one child record when parent record is initially created
I think I''ve improved on it and made it transactional (please correct me if I''m wrong). My new code look as follows: @user = User.new(params[:user]) @user.user_sites << UserSite.new(:site_id => @current_site.id) if @user.save ... I''m using the new method instead of the create on the UserSite object. From what I could tell in the docs, when done this way, the creation of the user (parent record) and the usersite (child record) are automatically included in a transaction as an atomic unit, so I shouldn''t have to do anything more. As I said above please correct me if I''m misunderstanding something here. On Aug 29, 8:53 pm, Chris <cdellin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am using Rails 3. I got it to work with the following code, but it > is not currently transactional (if the creation of a UserSite fails > the original user record will still exist in the db). > > @user = User.new(params[:user]) > if @user.save > -tENV8f5wrGRQFI55V6+gNQ@public.gmane.org_sites << UserSite.create(:site_id => @current_site.id) > ... > end > > While this works I''m not sure if there is a better way to accomplish > this (some Rails goodness I''m not familiar with). If anyone has an > opinion on it, please let me know. > > On Aug 29, 2:33 pm, Bill Walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Sun, Aug 29, 2010 at 12:34 PM, Chris <cdellin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > How would I handle a failure in that second piece? Can I put it in > > > transaction and roll it back? > > > Probabl, though you''d have to throw an exception for a transaction to > > be effective. IIRC, Rails3 has support for creating associated > > records built in but I haven''t gotten around to working with 3 and you > > didn''t say what version you''re using. > > > The easiest, most readable thing to do might be to just, in the controller... > > > if @user.save and @user.user_site > > .... > > > It''s probably better, though, to move it into the User model''s > > after_create (untested code) > > > after_create :create_default_user_site_rec > > > def create_default_user_site_rec > > user_site_rec = UserSite.new > > user_site_rec.user_id = self.id > > unless user_site.save > > self.destroy > > false > > end > > end > > > Tricky thing here is I''m not sure off the top of my head if the > > failure of the after_create and its returning false will translate > > into the save itself returning false. Sorry I don''t have the time to > > run it to ground for you. Hopefully this will give you some options > > to investigate. > > > Best regards, > > Bill > > > end-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.