Forum: Ruby on Rails optional has_one association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fb0d15e38835feca105e9741aa14cfe5?d=identicon&s=25 Guest (Guest)
on 2006-03-23 15:59
Hi!

I have a has_one association which is used for extending an entity,
like:

class User < ActiveRecord::Base
  has_one :user_ex

  #name, email
end

class UserEx < ...
  #country, city, street...
end

The UserEx entity is optional (there are users without extended info).
My problem is: if u is a User object which has no user_ex extension,
activerecord always tries to fetch the UserEx object whenever I
write u.user_ex (it thinks user_ex isn't yet loaded). I also want to
include the user_ex association when I load some users. What is
the simplest way to do this?

Thanks,
Adam
This topic is locked and can not be replied to.