Leo
Leo

Reputation: 2103

How to execute mysql query?

How can I use my custom query to look for records in Rails (MySQL)? I have a little bit more complicated query, including join and I don't know how to use it? I stumpled upon "find_by_sql" method but it is pointless since its returning Array insted of ActiveRecord::Relation.

EDIT my query looks similar to this:

"select students.id, students.school_id from students join schools on schools.id = students.school_id where schools.name LIKE '%#{search}%'"

Upvotes: 1

Views: 110

Answers (1)

Eugene
Eugene

Reputation: 4879

I believe something like this should work, assuming you are using Student and School for your model names:

Student.joins(:school).where("schools.name LIKE '%#{search}%")

Upvotes: 2

Related Questions