0

I have this scope with the parameter city, the scope receives the parameter, joins a table call Restaurant (because I have the param there) and then it makes a where condition with IN for multiple OR.

scope :by_cities, -> (city) { joins(:restaurant).where('restaurants.city IN (?)', city) }

The thing is that I want to learn how the scope can receive an array of param in the scope, I already have tried a lot of stuff and I'm taking the guide of this guy: Rails 4 scope with argument and this: ActiveRecord where field = ? array of possible values

Logs

Parameters: {"city"=>"SanPedro"} SELECT "vacancies".* FROM "vacancies" INNER JOIN "restaurants" ON "restaurants"."id" = "vacancies"."restaurant_id" WHERE (restaurants.city IN ('SanPedro'))

Route

http://localhost:3000/v1/vacancies?city=SanPedro&Monterrey

But, don't understand very well how to do it, anyone knows about this?

Daniel Disblu
  • 203
  • 2
  • 12
  • "don't understand very well how to do it" - but you already did it. Use it like this: `MyModel.by_cities([city1, city2])` – Sergio Tulentsev Aug 08 '18 at 22:24
  • doesn't work that way, I already tried, when I write `MyModel.by_cities('New York', 'San Fransisco')` I get this error: `ArgumentError: wrong number of arguments (given 2, expected 1)` – Daniel Disblu Aug 08 '18 at 22:31
  • 1
    Have you tried `scope :by_cities, -> (*cities) { joins(:restaurant).where('restaurants.city IN (?)', cities) }`? BTW, the [docs](https://guides.rubyonrails.org/active_record_querying.html#scopes) state *"Using a class method is the preferred way to accept arguments for scopes."* – jvillian Aug 08 '18 at 22:42
  • I already try that too, fortunately, I don't get an error but when I'm getting the consult in the database I don't get all the info, only one city, the return of the logs was this: `SELECT "vacancies".* FROM "vacancies" INNER JOIN "restaurants" ON "restaurants"."id" = "vacancies"."restaurant_id" WHERE (restaurants.city IN ('Monterrey'))` and I want the same but with multiple values, like this: `SELECT "vacancies".* FROM "vacancies" INNER JOIN "restaurants" ON "restaurants"."id" = "vacancies"."restaurant_id" WHERE (restaurants.city IN ('Monterrey, SanPedro'))` – Daniel Disblu Aug 08 '18 at 23:00
  • I update the info @jvillian – Daniel Disblu Aug 08 '18 at 23:07
  • 1
    "when I write `MyModel.by_cities('New York', 'San Fransisco')`" - but that's not what i wrote. My example has an array, yours doesn't. – Sergio Tulentsev Aug 09 '18 at 07:31

1 Answers1

2

Minimally, there is a problem with how you're forming your url query, here:

http://localhost:3000/v1/vacancies?city=SanPedro&Monterrey

As you can see in your params:

Parameters: {"city"=>"SanPedro"}

Your not getting an array of cities. Try something more like:

http://localhost:3000/v1/vacancies?city%5B%5D=San+Pedro&city%5B%5D=Monterrey

Which should give you something like:

Parameters: {"city"=>['San Pedro', 'Monterrey']}

BTW, you can see how a proper query string should look by doing:

{city: ['San Pedro', 'Monterrey']}.to_query 

in your console.

Then, you should be able to do something like:

Vacancy.by_city(params[:city])
jvillian
  • 19,953
  • 5
  • 31
  • 44