First, you have declared that User
has_one :e_user
. Thus, the correct relationship lookup is:
first_user = User.first
first_user.e_user
Second, the where
ActiveRecord method returns an ActiveRecord::Relation
object type. What you need is User
object itself. Instead, use find
:
a = User.find(1)
a.e_user
An explanation: the where
method returns an ActiveRecord::Relation
object which contains all class objects which meet the conditions in an array-like structure. However, even if where
returns just a single record, that record will still be contained within that array-like structure; any instance methods on the retrieved class won't be accessible until you access the object within the ActiveRecord::Relation
.
find
, on the other hand, returns the first object itself that meets the specified condition (by default, ActiveRecord matches against the object id
). Thus, any object returned by the find
method has full utility and use over the instance methods and attributes defined for those class objects:
find_user = User.find(1)
where_user = User.where(:id => 1)
find_user.class
#=> User
where_user.class
#=> ActiveRecord::Relation
find_user == where_user
#=> false
find_user == where_user.first
#=> true