szimek
2010-Oct-06 08:19 UTC
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.