# Creating has\_one models on save

**URL:** <https://www.ruby-forum.com/t/creating-has-one-models-on-save/164411>\
**Category:** Rails\
**Created:** [April 1, 2009, 3:48am UTC](https://www.ruby-forum.com/t/creating-has-one-models-on-save/164411 "2009-04-01T03:48:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![novaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/848f3c/32.png) [@novaruby](https://www.ruby-forum.com/u/novaruby)\
**Post date:** [April 1, 2009, 3:48am UTC](https://www.ruby-forum.com/t/creating-has-one-models-on-save/164411/1 "2009-04-01T03:48:13Z")

</div>

What is the best practice to create has\_one (always exist) model  
relationships when creating the parent model.

eg:  
class User \< ActiveRecord::Base  
has\_one :balance  
end

class Balance \< ActiveRecord::Base  
belongs\_to :user  
end

A User always has a Balance

I would like to create the Balance model when the User is saved.

There is 3 (maybe more choices to accomplish this), which one is the  
best practice:

1. Create the Balance model and associate it to the User from the  
controller that is creating the User model.

2. Do it in the User model with after\_create :create\_balance

3. Do it in a UserObeserver:

class UserObserver \< ActiveRecord::Observer  
def after\_create(user)  
balance = Balance.new(:amount =\> 0)  
balance = customer  
balance.save  
end  
end

Thanks! If this has been covered before, sorry, but just not finding  
the right search terms to limit results.

---

<div class="post-metadata">

**Author:** ![novaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/848f3c/32.png) [@novaruby](https://www.ruby-forum.com/u/novaruby)\
**Post date:** [April 1, 2009, 4:44am UTC](https://www.ruby-forum.com/t/creating-has-one-models-on-save/164411/2 "2009-04-01T04:44:22Z")

</div>

On Tue, Mar 31, 2009 at 6:48 PM, Albert W. \<  
removed\_email\_address@domain.invalid\> wrote:

> belongs\_to :user  
> controller that is creating the User model.  
> end  
> end
> 
> Thanks! If this has been covered before, sorry, but just not finding  
> the right search terms to limit results.

Albert, you should be able to do the following:

class User \< ActiveRecord::Base

after\_save :create\_balance

protected

def create\_balance

```
 balance = Balance.new( :amount => 0 )

# fill in any additional attributes

balance.save!

self.balance = balance # associate the balance to the user

```

end

end

BTW, you can use an after\_save filter to invoke the create\_balance  
method  
when you save the

user instance.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![novaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/848f3c/32.png) [@novaruby](https://www.ruby-forum.com/u/novaruby)\
**Post date:** [April 1, 2009, 7:48am UTC](https://www.ruby-forum.com/t/creating-has-one-models-on-save/164411/3 "2009-04-01T07:48:32Z")

</div>

Thanks Conrad…

I think after\_create is the better filter to use, otherwise the Balance  
is reset to 0 everytime there is an update to the User.

---

<div class="post-metadata">

**Author:** ![novaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/848f3c/32.png) [@novaruby](https://www.ruby-forum.com/u/novaruby)\
**Post date:** [April 1, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/creating-has-one-models-on-save/164411/4 "2009-04-01T14:48:29Z")

</div>

Albert W. wrote:

> What is the best practice to create has\_one (always exist) model  
> relationships when creating the parent model.

In addition to the technical answers…

Isn’t the relation “always has one” really the relation “is part of”?

Why all the fields are not in the same record?
