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?

6
  • "don't understand very well how to do it" - but you already did it. Use it like this: MyModel.by_cities([city1, city2]) Commented Aug 8, 2018 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) Commented Aug 8, 2018 at 22:31
  • 1
    Have you tried scope :by_cities, -> (*cities) { joins(:restaurant).where('restaurants.city IN (?)', cities) }? BTW, the docs state "Using a class method is the preferred way to accept arguments for scopes." Commented Aug 8, 2018 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')) Commented Aug 8, 2018 at 23:00
  • 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. Commented Aug 9, 2018 at 7:31

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response, it helps me, I didn't know about to_query, it will help me a lot in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.