# Has\_one. Deleting orphan tuples

**URL:** <https://www.ruby-forum.com/t/has-one-deleting-orphan-tuples/190823>\
**Category:** Rails\
**Created:** [June 28, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/has-one-deleting-orphan-tuples/190823 "2010-06-28T20:26:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![polunochnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/65b543/32.png) [@polunochnik](https://www.ruby-forum.com/u/polunochnik)\
**Post date:** [June 28, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/has-one-deleting-orphan-tuples/190823/1 "2010-06-28T20:26:38Z")

</div>

Hi.

How to delete orphan tuples after a new object has been created through  
has\_one association?

Here’s an example:

# a user model

class User \< ActiveRecord::Base  
Â Â has\_one :account  
end

# an account model

class Account \< ActiveRecord::Base  
Â Â belongs\_to :user  
end

# a users table

idÂ Â Â = 1  
name = Pavel

idÂ Â Â = 2  
name = Elena

# an accounts table

idÂ Â Â Â Â Â = 1  
user\_id = 1  
typeÂ Â Â Â = ‘silver’

idÂ Â Â Â Â Â = 2  
user\_id = 2  
typeÂ Â Â Â = ‘gold’

Then, if I create a new account for the first user an orphan tuple will  
stay in the accounts table:

> > User.find(1).create\_account(:type =\> ‘gold’)

# an accounts table

# an orphan tuple

idÂ Â Â Â Â Â = 1  
user\_id = null  
typeÂ Â Â Â = ‘silver’

idÂ Â Â Â Â Â = 2  
user\_id = 2  
typeÂ Â Â Â = ‘gold’

# a new tuple

idÂ Â Â Â Â Â = 3  
user\_idÂ = 1  
typeÂ Â Â Â = ‘gold’

So, how can I delete the tuple with id = 1 right after creating a new  
account for the first user?

Thanks.

RubyÂ Â 1.9.2;  
Rails 2.3.8.
