The Rails ORM (ActiveRecord) is built atop of a relational algebra gem (arel, which was eventually pulled into the AR library). I'm pretty sure the arel functions are considered private API, so it's not necessarily best practice, but you have the option to call those functions directly when you find the ORM lacking and don't want to pass around raw SQL.