1

I have an issue in finding the correct records from the database. This is my query:

@interested_individual = Profile.where('category_id = ?', @event.interests).all

Where category_id is the column in the profiles table what holds the integer value and @event.interests returns an array that is ["1", "2", "3"]

What I want here is to fetch all the profiles where its category_id is present in the @event.interests array. Please let me know the solution. Thanks in advance

3 Answers 3

3

You don't really need to manually write the binding with ? and all.

Try this:

@interested_individual = Profile.where(category_id: @event.interests).all

Active Record will then turn that into category_id IN (?) for you if you pass in an array.

Sign up to request clarification or add additional context in comments.

Comments

2

This query will translate to category_id IN [interest_ids...]

Profile.where(category_id: @event.interests).all

Comments

1

You were close. Use IN.

@interested_individual = Profile.where('category_id IN (?)', @event.interests).all

But, I would suggest find you here, if you are only wanted to get collection of all Profile records :

@interested_individual = Profile.find(@event.interests)

Comments

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.