Tuesday, 15 March 2011

ruby on rails - Ransack: how to join table multiple times with different alias? -


Let's say I have: with has_many cooperation with objects: properties, then I can search for all the objects that have the property 'A_name' and the value 'a_value' like this

  Q: {properties_name_eq: 'a_name', attribute_ value_eq: 'a_value'}  

Now if I want to find all the items with an asset with the name 'a_name' and 'a_value' Is there a property with the name 'a_value' and the value 'another_value'?

The following does not work once the properties are included in the table

  q: {g: {'0' => {Properties_name_eq: 'a_name', properties_value_eq: 'a_value'}, '1' = & gt; {Properties_name_eq: 'another_name', properties_value_eq: 'another_value'}}}  

Generated SQL sees something like

  select "difference" item ". * Exclude "item" and "property" on "property" "item" "item". "Id" = "a_value") and "id" inner join (("item" from "item")) sel_111 sel_111.id WHERE ("property". "Name" = 'a_name' and 'properties'.) And ( "Property". "Name" = 'other_name' and "property". "Value" = 'other_ value')) Edit  

:

What do I do after this To make it more clear, I will paste a specific description below.

  items. Create: Name: 'A', attribute_editriz: [{name: 'a', value: 'a1'}, {name: 'b', value: 'b1'}] item. Create name: 'a', properties_attributes: [{name: 'a', value: 'a1'}] item Create Name: 'B', Properties_A Contribution: [{Name: 'B', Value: 'B1'}] Items Create Name: 'Ax', Properties_attributes: [{Name: 'a', Value: 'a1'}, {name: 'b', Value: 'x'}] Items Create Name: 'bx', p Roperties_attributes: [{name: 'a', value: 'x'}, {name: 'b', value: 'b1'}] item. Build Name: 'other', properties_attributes: [{name: 'other' value: '123'}}: index: q: {properties_name_eq: 'a', properties_value_eq: 'a1'} name = JSON.parse ( Response.body) .map {| U | U ['name']} is expected (name). Match_aray ['now', 'a', 'ax']] # Okay! Get: Index, q: {m: 'or', g: {'0' => {Properties_name_eq: 'a', properties_value_eq: 'a1'}, '1' = & gt; {Properties_name_eq: 'b', properties_value_eq: 'b1'}}} name = JSON.pres (response.body) .map {| U | U ['name']} is expected (name). To match_array ['ab'] #FAILS!  

just use Model.search (params [: q] Try: (merg, m: 'or')) , using your example:

  q: {m: 'or', g: {'0' = & Gt; {Properties_name_eq: 'a_name', properties_value_eq: 'a_value'}, '1' = & gt; {Properties_name_eq: 'another_name', properties_value_eq: 'another_value'}}}  

You can find more information

You need or Is where your query level, because properties.name can not be 'a_name' and 'other_name' at the same time. The table does not require a second alias.


No comments:

Post a Comment