这是具有 1 对 1 关联的两个模型的代码
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
下面是第二个模型:
class EUser < ActiveRecord::Base
belongs_to : user
end
当我去 Rails 控制台并从 db 获取用户时,通过
a = User.where(:id => 1)
我得到了一个用户。现在我想获取与 a 关联的 e_user(如果有的话,它应该返回它或返回 null),但是当我在控制台中键入 a.EUser 时收到错误消息,错误是
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>'
问候