简体   繁体   中英

Rails filtering with scopes not returning results from multiple paramters

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM