Here is code of two models which have 1 to 1 association
class User < ActiveRecord::Base
has_one :e_user
validates_presence_of :first_name
validates_presence_of :last_name
validates :password, presence: true, :length => { :minimum => 6}
validates_uniqueness_of :email, :message => ": This email is already registered!"
validates_presence_of :email
end
Below is second model:
class EUser < ActiveRecord::Base
belongs_to : user
end
When i go to rails console and get a User from db, by doing
a = User.where(:id => 1)
I get a user in a. Now I want to get the e_user associated with a(if any it should return it or return null), but i get error message when i type a.EUser in console, error is
NoMethodError: undefined method `EUser' for #<ActiveRecord::Relation:0x00000002ab5908>from /home/faraz/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.1.1/lib/active_record/relation.rb:459:in `method_missing'
from (irb):3
from /home/faraz/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /home/faraz/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /home/faraz/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Regards
First, you have declared that
Userhas_one :e_user. Thus, the correct relationship lookup is:Second, the
whereActiveRecord method returns anActiveRecord::Relationobject type. What you need isUserobject itself. Instead, usefind:An explanation: the
wheremethod returns anActiveRecord::Relationobject which contains all class objects which meet the conditions in an array-like structure. However, even ifwherereturns 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 theActiveRecord::Relation.find, on the other hand, returns the first object itself that meets the specified condition (by default, ActiveRecord matches against the objectid). Thus, any object returned by thefindmethod has full utility and use over the instance methods and attributes defined for those class objects: