Home > Net >  Rails filtering with scopes not returning results from multiple paramters
Rails filtering with scopes not returning results from multiple paramters

Time:11-17

I have the following scope defined in the model:

  scope :filter_by_language, -> (language) { where( languages: {language_name: language}) }

and in the controller I use

@things= @things.filter_by_language(params[:language]).includes(:language) if params[:language].present?

and this works for a single query like so:

http://127.0.0.1:3000/api/v1/things?language=French

but my research suggests that I should be able to return results from multiple languages by using & in the get request to return all the matching results, so this:

http://127.0.0.1:3000/api/v1/things?language=French&language=English

but I only the results from the last parameter, so in this case only English results, not English and French.

Can anyone point me in the direction I am going wrong? many thanks!

CodePudding user response:

You'll need to send those non-escalar parameters using square brackets, like:

?language[]=French&language[]=English

Documentation: https://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

  • Related