2017-03-15 123 views
0

我想查询2表,凭据和集成。我想检索每个凭据记录WHERE user_id == current_user.id。我也想检索每个集成记录WHERE name == "Twitter"Rails加入多个条件的查询

正下方运行,并输出[[4, "Twitter"][3,"YouTube"]]

Credential.joins(:integration).where(user_id: current_user.id).pluck(:id, :name) 

查询直接下面的查询应该运行并产生[4, "Twitter"]而是它产生一个错误。

Credential.joins(:integration).where(user_id: current_user.id).where(integration: {name: 'Twitter'}).pluck(:id, :name) 

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "integration" 
LINE 1: ...ration_id" WHERE "credentials"."user_id" = $1 AND "integrati... 

最终,我试图将我在其他代码中使用的集成列入白名单。如果我在连接参数.joins(:integrations)中使用复数,Rails拒绝查询。

+0

“哪里(整合:{..})'工作,与复数? – Iceman

+1

哇。这工作!谢谢!如果你发布解决方案,我会给你点数! – HashRocketSyntax

+0

很高兴工作,发布了一个答案 – Iceman

回答

1

在您的where子句中使用复数形式。

where(integrations: {..}) 

Credential.joins(:integration) 
    .where(user_id: current_user.id) 
    .where(integrations: {name: 'Twitter'}) 
    .pluck(:id, :name)