# Associated object is not set correctly after creating with belongs\_to and has\_one associations

**URL:** <https://www.ruby-forum.com/t/associated-object-is-not-set-correctly-after-creating-with-belongs-to-and-has-one-associations/196072>\
**Category:** Rails\
**Created:** [October 6, 2010, 10:20am UTC](https://www.ruby-forum.com/t/associated-object-is-not-set-correctly-after-creating-with-belongs-to-and-has-one-associations/196072 "2010-10-06T10:20:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [October 6, 2010, 10:20am UTC](https://www.ruby-forum.com/t/associated-object-is-not-set-correctly-after-creating-with-belongs-to-and-has-one-associations/196072/1 "2010-10-06T10:20:32Z")

</div>

Hey,

I’ve got 2 models: User and Account::Website that look like this:

class User \< ActiveRecord::Base  
has\_one :website\_account, :class\_name =\>  
“Account::Website”, :dependent =\> :destroy  
end

class Account::Website \< ActiveRecord::Base  
belongs\_to :user, :touch =\> true

before\_validation :assign\_new\_user, :if =\> :user\_missing?

protected

def assign\_new\_user  
self.user = User.new  
end

def user\_missing?  
user.blank?  
end  
end

The problem is with the following code:

website\_account = Account::Website.new  
website\_account.save  
website\_account.user =\> returns persisted  
User object  
website\_account.user.website\_account =\> nil

If I reload the user object, then it correctly returns website  
account, but shouldn’t it be set automatically by Rails?

Cheers,  
Szymon
