早上好溢出。
我在尝试选择全部时遇到问题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