我有一个属于用户的模型电子邮件地址。在我的用户模型中,它说它有很多电子邮件地址。考虑以下控制台输出:
irb(主):073:0>
b = EmailAddress.find(2)
EmailAddress Load (0.4ms) SELECT email_addresses.* FROM email_addresses WHERE email_addresses.id = 2 LIMIT 1
=> #EmailAddress id: 2, email: "w@example.com", user_id: 2, created_at: "2013-03-06 02:33:40", updated_at: "2013-03-06 02:33:40 "
irb(主):074:0>
b.user
用户负载 (0.1ms) SELECT users.* FROM users WHERE users.id = 2 LIMIT 1
=> #用户ID:2,用户名:“w”,created_at:“2013-03-06 02:33:40”,updated_at:“2013-03-06 02:33:40”
irb(主):075:0>
b = EmailAddress.where(:email => "r@example.com")
EmailAddress Load (0.1ms) SELECT email_addresses.* FROM email_addresses WHERE email_addresses.email = r@example.com
=> #EmailAddress id: 1, email: "r@example.com", user_id: 1, created_at: "2013-03-05 07:26:56", updated_at: "2013-03-05 07:26:56 "
irb(主):076:0>
b.user
NoMethodError:未定义的方法用户来自/var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:45:in `method_missing' from (irb):76
有人可以告诉我为什么当我使用时where()
我无法找到用户,但是当我使用 find() 时它可以工作?这是非常令人困惑的输出。