如果记录具有数组中的任何 ID,则选择所有记录

2024-01-05

早上好溢出。

我在尝试选择全部时遇到问题Treatments将任何 ID 存储在名为的数组中@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”运算符可能不是我想要做的事情所需的 - 我尝试使用 ANY 运算符,但也无济于事。问题是否源于它是一个整数数组?

治疗模型。

class Treatment < ActiveRecord::Base

  has_and_belongs_to_many :remedy

end

补救模型。

class Remedy < ActiveRecord::Base

  has_and_belongs_to_many :treatments, dependent: :destroy
end

有一个后续帖子解决了我的问题here https://stackoverflow.com/questions/37416290/select-record-if-the-records-has-same-id-as-any-of-the-ids-in-the-array/37416618#37416618


通过 ids 数组选择记录是通过查询完成的:

Record.where(id: ids)

where ids是ids数组。可以用另一个查询替换它。

对于您的情况,按比赛名称选择补救措施如下:

ids = Remedy.where("LOWER(remedyName) LIKE ?", name.downcase).pluck(:id)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果记录具有数组中的任何 ID,则选择所有记录 的相关文章

随机推荐