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.