Msencenb
Msencenb

Reputation: 5104

Dealing with multiple SQL queries within single find_by_sql

Please note that I understand I could do the following in a single SQL query that's not the point of the question however... I'm more curious about how rails deals with multiple queries.

Let's say I have a Movies model with attributes such as title, director, id, star, release date, etc.

Now I have @Movies = find_by_sql("select * from movies where genre='Action';select * from movies where genre='Comedy';")

And I iterate through it on the view. However only the first query results seem to be contained in the answer. Just curious about how this works.

Thanks

Upvotes: 0

Views: 788

Answers (2)

John Doe
John Doe

Reputation: 1

Try the UNION SELECT... sql query...

Also, LOL cs142?

Upvotes: 0

David Sulc
David Sulc

Reputation: 25994

This should work

@Movies = find_by_sql("select * from movies where genre='Action' or genre='Comedy';")

Edit: this is maybe what you're refering to with the single query.

Another way to do it is

find_by_genre('Action') << find_by_genre('Comedy')

Upvotes: 1

Related Questions