活动记录where子句中没有这样的列

时间:2015-12-27 02:31:18

标签: ruby-on-rails sqlite activerecord rails-activerecord

Match.where(approval: {summoner_id: 18})
Match Load (0.6ms)  SELECT "matches".* FROM "matches" WHERE "approval"."summoner_id" = ?  [["summoner_id", 18]]
SQLite3::SQLException: no such column: approval.summoner_id: SELECT "matches".* FROM "matches" WHERE "approval"."summoner_id" = ?
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: approval.summoner_id: SELECT "matches".* FROM "matches" WHERE "approval"."summoner_id" = ?

但如果我这样做

Approval.first.summoner_id
Approval Load (0.2ms)  SELECT  "approvals".* FROM "approvals"  ORDER BY "approvals"."id" ASC LIMIT 1
#=> 18

出现了。

我正在尝试找到所有匹配,其中“匹配”的批准具有特定的召唤者ID。

如何搜索?

class Match < ActiveRecord::Base
  belongs_to :approval
end

1 个答案:

答案 0 :(得分:3)

您正在尝试引用approval表中的列:

:approval => { summoner_id: 18 }

但是你犯了两个错误:

  1. 该表名为approvals,而不是approval
  2. 您没有加入,因此查询无法使用approvalapprovals表。
  3. 我想你的意思是说:

    Match.joins(:approvals).where(:approvals => {summoner_id: 18})