选择所有记录,如果记录有任何的ID从阵列 [英] Selecting all Records if Record has any of the ID's from Array

查看:172
本文介绍了选择所有记录,如果记录有任何的ID从阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早安溢出。

我有一个问题想选择所有治疗有任何的ID存储在一个名为数组 @problem

I'm having a problem trying to select all Treatments that have any of the ID's stored in an array called @problem.

这是我处理的控制器。

@problem = Remedy.find_by_sql(["SELECT id FROM remedies WHERE LOWER(\"remedyName\") LIKE?", "%#{params[:searchremedy]}%".downcase])

 query = "SELECT * FROM treatments INNER JOIN remedies_treatments on treatments.id = remedies_treatments.treatment_id WHERE remedies_treatments.treatment_id LIKE ?"
 @pretreatments = Treatment.find_by_sql([query, @problem])

这是错误从控制台

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near ","
LINE 1: ...d WHERE remedies_treatments.treatment_id LIKE 233,234,224

在'LIKE'运营商可能不是什么我需要什么,我希望做的 - 我尝试使用任何运营商,但无济于事无论是。是否从事实的问题,干它是一个整数数组?

The 'LIKE' operator is probably not what I need for what I'm looking to do - I tried using the ANY operator but to no avail either. Does the problem stem from the fact it is an array of integers?

治疗模式。

class Treatment < ActiveRecord::Base

  has_and_belongs_to_many :remedy

end

的补救模型。

class Remedy < ActiveRecord::Base

  has_and_belongs_to_many :treatments, dependent: :destroy
end

有一个跟进帖子里我的问题就解决了​​<一个href=\"http://stackoverflow.com/questions/37416290/select-record-if-the-records-has-same-id-as-any-of-the-ids-in-the-array/37416618#37416618\">here

There is a follow up post where my issue was resolved here

推荐答案

由IDS阵列选择记录与查询完成:

Selecting record by ids array is done with a query:

Record.where(id: ids)

其中, IDS 是ids数组。它可以与其他查询来代替。

where ids is ids array. It can be replaced with another query.

有关你的情况通过比赛名称选择补救措施如下:

For your case selecting remedies by match name will be as follows:

ids = Remedy.where("LOWER(remedyName) LIKE ?", name.downcase).pluck(:id)

这篇关于选择所有记录,如果记录有任何的ID从阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆