user2945241
user2945241

Reputation: 360

array as arguments in rails

Hello i want to pass array as few arguments in rails.

here is the code i wish to run, but i get to few arguments error

query_string = "date_to > ? AND date_from < ?"
var_array = [date_from, date_to]

CreditType.find(type).formulas.where(query_string, var_array.each).exists? // is there a way to loop trought array and pass all elements as arguments?

Is this even possible? i tried different kinds of ways, but nothing seems to work, i need this peace of code to be dynamic. query_string and var_arrays could change. so is there a way to accomplish this? Thank you :)

Upvotes: 3

Views: 59

Answers (1)

Paul G&#246;tze
Paul G&#246;tze

Reputation: 374

You can pass all entries of an array as comma separated params by using the so called splat operator * followed by the array.

Try this:

 CreditType.find(type).formulas.where(query_string, *var_array).exists?

Upvotes: 4

Related Questions