Hello, I have two classes that are self explanatory and are listed below. class User < ActiveRecord::Base belongs_to :account end class Account < ActiveRecord::Base has_one :account_owner, { :dependent => true, :class_name => "User", :conditions => "is_account_owner = 1" } has_many :users end In the signup controller when an account is created one user is created as well that is the account_owner (similar to how Basecamp does it). My question is how to ensure that either the account is created and the user is created or nothing is saved. It seems like the only way to do this is to save the Account object then save the User object. Is there a best practices way of doing this? Essentially the sql you want to spit out is: start transaction; insert into accounts (...) values (...); insert into users (account_id, ...) values (last_insert_id(), ...); commit; Thanks, Zack
The following works in a has_many relationship. I believe will work for has_one as well: Assuming you params[:account] returns a hash that maps to Accounts as well to User, in account_controller: def create @account = Account.new(params[:account]) @account.account_owner.create(params[:account]) @account.save ... end Okada. 2006/1/4, Zack Chandler <zackchandler@drainpatrol.com>:> > > Hello, > > I have two classes that are self explanatory and are listed below. > > class User < ActiveRecord::Base > belongs_to :account > end > > class Account < ActiveRecord::Base > has_one :account_owner, { :dependent => true, :class_name => "User", > :conditions => "is_account_owner = 1" } > has_many :users > end > > In the signup controller when an account is created one user is created as > well that is the account_owner (similar to how Basecamp does it). My > question is how to ensure that either the account is created and the user > is > created or nothing is saved. > > It seems like the only way to do this is to save the Account object then > save the User object. Is there a best practices way of doing this? > > Essentially the sql you want to spit out is: > > start transaction; > insert into accounts (...) values (...); > insert into users (account_id, ...) values (last_insert_id(), ...); > commit; > > > Thanks, > Zack > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060105/a6abc97b/attachment.html
Hi Zack, On 5.1.2006, at 4.06, Zack Chandler wrote:> > Hello, > > I have two classes that are self explanatory and are listed below. > > class User < ActiveRecord::Base > belongs_to :account > end > > class Account < ActiveRecord::Base > has_one :account_owner, { :dependent => true, :class_name => "User", > :conditions => "is_account_owner = 1" } > has_many :users > end > > In the signup controller when an account is created one user is > created as > well that is the account_owner (similar to how Basecamp does it). My > question is how to ensure that either the account is created and > the user is > created or nothing is saved. > > It seems like the only way to do this is to save the Account object > then > save the User object. Is there a best practices way of doing this?Transaction, baby: User.transaction do # the code that creates both user and account here (Okada already touched this) end //jarkko> > Essentially the sql you want to spit out is: > > start transaction; > insert into accounts (...) values (...); > insert into users (account_id, ...) values (last_insert_id(), ...); > commit; > > > Thanks, > Zack > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2363 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060105/05db2ce7/smime.bin
For anyone having a similar problem hopefully this will help. BTW... Thanks to Carlos and Jarkko for your suggestions. There is the test snippet of code that did the trick: @account = Account.new :account_name => "Dev account" @account.build_account_owner :username => "test_user", :password => "password", :name => "Test User", :email_address => "test@fake.com", :is_account_owner => 1 if @account.valid? && @account.account_owner.valid? @account.save end This does result in the desired transaction BEGIN INSERT INTO accounts ... INSERT INTO users ... COMMIT The valid? check must be done because ActiveRecord will happily save the account but not the user if there are validation problems on the user object. Hope this helps. Zack -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Carlos Y. Okada Sent: Thursday, January 05, 2006 4:13 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] has_one :dependent => true question The following works in a has_many relationship. I believe will work for has_one as well: Assuming you params[:account] returns a hash that maps to Accounts as well to User, in account_controller: def create ? @account = Account.new(params[:account]) ? @account.account_owner.create(params[:account]) ? @account.save ... end Okada. 2006/1/4, Zack Chandler < zackchandler@drainpatrol.com>: Hello, I have two classes that are self explanatory and are listed below. class User < ActiveRecord::Base ??belongs_to :account end class Account < ActiveRecord::Base ??has_one :account_owner, { :dependent => true, :class_name => "User", :conditions => "is_account_owner = 1" } ??has_many :users end In the signup controller when an account is created one user is created as well that is the account_owner (similar to how Basecamp does it).??My question is how to ensure that either the account is created and the user is created or nothing is saved. It seems like the only way to do this is to save the Account object then save the User object.??Is there a best practices way of doing this? Essentially the sql you want to spit out is: start transaction; ??insert into accounts (...) values (...); ??insert into users (account_id, ...) values (last_insert_id(), ...); commit; Thanks, Zack _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails