Associated object is not set correctly after creating with belongs_to and has_one associations

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs