Saturday 15 May 2010

database - Rails, passing an array as criteria to query -


I'm calling the database based on the criteria passed from the scene, so it must be dynamic.

Say I have 2 arrays:

  column = ['col1', 'col2', 'col3'] vals = ['val1', 'val2 'Val3']   

The query is easy to create, I can concatenation, such as

  query = columns [0] + "= ( ?) And "+ ..   

But what about the parameters?

  @finalValues ​​= MyTable.find (: all, terms => [query, wal [0], wall [1] ...])  < / Pre> 

But I do not know how many parameters I get. Therefore, when the query problem is resolved with the loop and the inclusion, I can do something like this: @finalValues ​​= MyTable.find (: All ,: conditions = & gt; [query, wal]) and the railroad will understand that I am not going to have an array for the IN (?) Segment, but to divide the values ​​for each person (?)

Or is my only option to do a single complete raw string? >

You can create condition as the first element with the query and add all the Vel element to it.

  query = columnss.map {| Col | "# {Col} =?"}. Join ("AND") @finalValues ​​= MyTable.find (: All, Conditions => [Queries, * vals])   

Point Alignment to Columns and Similar Values Must be number of elements.

No comments:

Post a Comment