0

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

1
  • i am not able to run multiple queries how can i do that?? Commented Nov 13, 2013 at 12:18

2 Answers 2

1

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

2 Comments

Yeah this was what I was referring to as a single query. Not really interested in getting the query results I want more interested in how rails deals with it.
I think "the rails way" would be the second one: fetch result for each category and return concatenated arrays. If you want only one DB trip, I think you'll have to use the single query (or a variation).
0

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

Also, LOL cs142?

1 Comment

Yes CS142 and yes union select is the correct way to do it. Since the multiple query was my first attempt though I am still curious about how Rails handles multiple queries with the find_by_sql command. Looks like it just stops after the first query?

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.