I’m trying build a conditions array to be using in a prepared statement:
Cars.find(:all, :conditions=>["color = ? AND doors = ? AND type = ?", "black", "4", "sedan"])
I’ve tried doing the following but getting an error of “ActiveRecord::PreparedStatementInvalid (wrong number of bind variables (4 for 2)”:
conditions = []
conditions += ["color = ?", "black"]
conditions += ["doors = ?", "4"]
conditions += ["type = ?", "sedan"]
Cars.find(:all, :conditions=>conditions)
What is the proper way of building a conditional for prepared statements?
The problem is that you are building a wrong query, since you pass the params to the questionmarks all the way through your statement.
Your resulting query looks something like this:
In order to achieve your desired array, you would have to do something like this.
If you repeat the last steps, you should get your required result.