Ruby on rails Ruby on Rails-ActiveRecord关系无法识别字段

Ruby on rails Ruby on Rails-ActiveRecord关系无法识别字段,ruby-on-rails,Ruby On Rails,我有一个应用程序的用户表,其中包含所有用户的列表。此表有一个名为active的布尔字段 我有以下代码来获取用户: existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase) user = User.where(email: auth_hash['info']['email'].downcase).first 这就是我在对现有用户进行检查时得到的结果。检查: 上述调用引发此错误: und

我有一个应用程序的用户表,其中包含所有用户的列表。此表有一个名为active的布尔字段

我有以下代码来获取用户:

existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase)
user = User.where(email: auth_hash['info']['email'].downcase).first
这就是我在对现有用户进行检查时得到的结果。检查:

上述调用引发此错误:

undefined method `active?' for #<User::ActiveRecord_Relation:0x00007f0a58b2c500> Did you mean? acts_like?
当existing_user.inspect显示active:true时,为什么上述调用existing_user.active失败?请帮忙

我想你应该用if!现有用户。第一个。活动?。这在你的情况下有效。Where子句返回一个数组,而不是一个对象。在您的例子中,现有的用户是数组而不是对象

这个答案与主题无关,但可以为您节省很多:

每次调用此现有的\u user=user.whereLOWERemail=?,都会验证\u hash['info']['email']。downcase,它将对表中的所有电子邮件进行downcase,并查找正确的电子邮件

我建议在保存用户之前,先将电子邮件降级,并在其上添加索引

然后获取用户:

existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase)
user = User.where(email: auth_hash['info']['email'].downcase).first
尝试这种方法,您将看到数据检索的巨大差异,现在是1.9ms

user = User.where(email: auth_hash['info']['email'].downcase).first