Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Sql Subquery in Rails
3 Posts
Sql Subquery in Rails

bold Hi All,

Can someone help me write an efficient subquery in rails?

I have a SAMPLE table .Sql query is as below . My need is update records sorted on id when I know num_id.

ID | num_id | ............

45 | 24 | 46 | 22 | 47 | 28 | 48 | 21 |

select * from SAMPLE where ID >= (select id from SAMPLE where num_id = 22 ) and ID <= ( select id from SAMPLE where num_id =21)

Regards, Naren bold

try this railscast episode http://railscasts.com/episodes/108-named-scope it has really some awesome rails 2.1 named scope features.

Thank you. I think named_scope allows us write custom queries for the model with the scope. My actual intention was to find update statement to deal directly with the subquery.


Sample.update_all( STATUS=0,condition)

where STATUS is another column of SAMPLE table

and condition " ID >= (select id from SAMPLE where num_id = 22 ) and ID

3 Posts
Login to add your message