Question

I have a long SQL query, which i am using in rails Model.find_by_sql, I have to add couple of conditions in that query like i wanted to fetch all the records whose parent.is_allowed is true.

That means i am fetching all the child elements and i wanted to use association which is between child and parent.And in the Child table we have the parent_id.

Thanks

Was it helpful?

Solution

You could try something like this:

Model.find_by_sql("select * from models where parent_id in (select id from parents where is_allowed='true') and ... ")

Just replace the ... with your original SQL query.

Edit

Based on your comment, I think this line is more what you need:

Model.find_by_sql("select * from models where parent_id in (select id from users where is_allowed='true')")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top