activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.67k stars 801 forks source link

Ransacker params with alias for Passing Arguments #1009

Open psoldier-robly opened 5 years ago

psoldier-robly commented 5 years ago

In 6. Passing arguments to a ransacker

The example to call a ransacker has full-name params

Person.ransack(
  conditions: [{
    attributes: {
      '0' => {
        name: 'author_max_title_of_article_where_body_length_between',
        ransacker_args: [10, 100]
      }
    },
    predicate_name: 'cont',
    values: ['Ransackers can take arguments']
  }]
)

:attributes, :predicate_name, :values don't work, it should be :a, :p, :v

Person.ransack(
  conditions: [{
    a: {
      '0' => {
        name: 'author_max_title_of_article_where_body_length_between',
        ransacker_args: [10, 100]
      }
    },
    p: 'cont',
    v: ['Ransackers can take arguments']
  }]
)
mateuszbialowas commented 1 year ago

Could you please explain why is that?

I created my own ransacked method in model, and now I try to pass args there. I also need to merge these params with params[:q]

m3thom commented 9 months ago

I confirm that you need to use the alias methods :a, :p, :v instead of the original methods :attributes, :predicate_name, :values if you want to use the conditions argument."

m3thom commented 9 months ago

@mateuszbialowas

You could do something like this.

# Assuming your `params[:q]`
# puts params[:q]
# {"id_eq": 1, title_eq: "Input title"}