# Has\_one :dependent =\> true question

**URL:** <https://www.ruby-forum.com/t/has-one-dependent-true-question/89669>\
**Category:** Rails\
**Created:** [January 5, 2006, 3:09am UTC](https://www.ruby-forum.com/t/has-one-dependent-true-question/89669 "2006-01-05T03:09:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 5, 2006, 3:09am UTC](https://www.ruby-forum.com/t/has-one-dependent-true-question/89669/1 "2006-01-05T03:09:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 5, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/has-one-dependent-true-question/89669/2 "2006-01-05T13:15:09Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/has-one-dependent-true-question/89669/3 "2006-01-05T17:44:32Z")

</div>

Hi Zack,

On 5.1.2006, at 4.06, Zack C. wrote:

> has\_one :account\_owner, { :dependent =\> true, :class\_name =\> “User”,
> 
> 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

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/has-one-dependent-true-question/89669/4 "2006-01-05T19:57:29Z")

</div>

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 =\> “removed\_email\_address@domain.invalid”,  
: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
