2

Trying to do the following

ElectricityProfile.find_by_sql ["select * from electricity_profiles where owner_id IN ?", array]

However seems that sql doesn't accept arrays.

Is there any other way to check column against array?

Using: Rails 3.2.x Ruby 193 MySQL

2 Answers 2

3

You need braces around the ?.

ElectricityProfile.find_by_sql ["select * from electricity_profiles where owner_id IN (?)", array]

or

ElectricityProfile.where("owner_id IN (?)", array)
Sign up to request clarification or add additional context in comments.

4 Comments

Sweet! The first one works, I don't want to use ActiveRecord for this purpose
You know that find_By_sql is also part of ActiveRecord right? apidock.com/rails/ActiveRecord/Base/find_by_sql/class
@DanMyasnikov by the way, the less you accept answer, the less you will have people answering your questions.
sorry mate, to much of rush
-2

Use the below.

           ElectricityProfile.find_by_sql("select * from electricity_profiles where owner_id in (?)", array])

3 Comments

Please update your answer as you don't use raw sql. It is ActiveRecord method
it's better to use ActiveRecord. Because it's more efficient.
You are wrong, ActiveRecord is nicer to use, however raw sql has better performance over the large chunk of data.

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.