Search code examples
ruby-on-railsransack

How does ransack take the union of two sets of parameters


How does ransack take the union of two sets of parameters

    q0 = {:field01_in=>[], :field02_in=>[], :fie4d03_in=>[value01,value02], :field02_in=
>[]}
    q1 = {:field01_in=>[], :field02_in=>[], :fie4d03_in=>[value03,value04], :field02_in=
>[value05]}

Solution

  • ransack support grouping queries by adding a combinator m: or (m: and) to the query hash. (default combinator is and)

    so if you want ransack take the union of two sets of parameters (i assume that you want to merge 2 results of query and of all q0 parameters and query and of all q1 parameters)

    q0 = {:field01_in=>[], :field02_in=>[], :fie4d03_in=>[value01,value02], :field02_in=
    >[]}
    q1 = {:field01_in=>[], :field02_in=>[], :fie4d03_in=>[value03,value04], :field02_in=
    >[value05]}
    
    union_params = {groupings: [q0, q1], m: 'or'}
    @q = Issue.ransack(union_params)
    @issues = @q.result