A
A
Alexander Petrov2017-01-13 15:17:51
Ruby on Rails
Alexander Petrov, 2017-01-13 15:17:51

How to filter by region?

The question is how to filter the data. The idea is this:
There are objects. They are tied to the districts of the city. For example, there are 6 districts.
The filter comes with an array of the id of those areas that need to be filtered. For example [2,4,5] - that is, the 2nd, 4th and 5th districts. If the object is located in one of these areas, then we output it, if not, then no.
The object has a connection in the area. Like this: object.region_id - id of the region.
I don't understand how to implement it.
Something like that should be result=Object.where(region_id: ar_region)
That is, you need to display objects if region_id is equal to at least one of the values ​​in the ar_region array

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Bordenyuk, 2017-01-13
@Mirkom63

WHERE-IN in rails is easy. The only thing you need to make sure is that the array values ​​are of the correct data type.

regions_list = params[:regions].map(&:to_i) # если params[:regions] уже массив, пришедший с формы-фильтра ["1", "2", "3"]
objects = Object.where(region_id: regions_list)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question