Rails 5-连接多态搜索-语法错误,意外的',',

时间:2018-08-07 05:51:06

标签: ruby-on-rails

我有以下查询:

folder.children.joins( "INNER JOIN folders ON items.itemable_id = folders.id" ).where( folders: { "key LIKE ?", "%#{params[:search]}%" } )

我收到以下错误,但我不确定为什么:

syntax error, unexpected ',', expecting => here( folders: { "key LIKE ?", "%#{params[:search]}%" } ) ^

 syntax error, unexpected '}', expecting keyword_end E ?", "%#{params[:search]}%" } ) ^

以下是搜索查询:

localhost:3000/api/1/folders/155.json?search=a

1 个答案:

答案 0 :(得分:1)

编辑:

您只需将整个条件添加到单个原始查询中

folder.children
  .joins("INNER JOIN folders ON items.itemable_id = folders.id" )
  .where("folders.key LIKE ?", "%#{params[:search]}%")

因此,这将创建一个以

结尾的查询
Select * FROM items 
INNER JOIN folders ON items.itemable_id = folders.id 
WHERE folders.key LIKE '%a%';