开发者

Wrong number of bind variables complication in Ruby

开发者 https://www.devze.com 2023-01-27 18:37 出处:网络
I have a table in which I get all the listings of the table through the Ruby Find Function. conditions = [Proje开发者_StackOverflow中文版ct.in_used_projects(:alias => \"projects\") + \" AND name L

I have a table in which I get all the listings of the table through the Ruby Find Function.

conditions = [Proje开发者_StackOverflow中文版ct.in_used_projects(:alias => "projects") + " AND name LIKE ? test"]
projects = Project.find(:all, :conditions => conditions)

But the moment I add "AND name LIKE ? test", I get wrong number of bind variables. The function Project.in_used_projects returns the value: projects.status = 2

I can't just change the Project.in_used_projects to project.status = 2 because the value is different for every project. The error I get is:

wrong number of bind variables (0 for 1) in: (projects.status = 2) AND name LIKE ? test

I know to bind variables the format needs to be: (Parameter, Parameter, Variable, Variable) but I need it to be (Paramters, Variable, Paramter, Variable)

Any ideas?


It seems to me you do have the wrong number of bind variables. You have 1 question mark, but 0 bind variables. Assuming you are looking for a name with "test" in it somewhere, you need to put "test" as a bind variable, or hard code it, like this (0 bind variables, 0 slots):

[Project.in_used_projects(:alias => "projects") + " AND name LIKE '%test%'"]

Or like this (1 bind variable, 1 slot):

[Project.in_used_projects(:alias => "projects") + " AND name LIKE ?", '%test%']
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号