How to chain raw SQL queries in Rails OR how to return an ActiveRecord_Relation from a raw SQL query in Rails? How to chain raw SQL queries in Rails OR how to return an ActiveRecord_Relation from a raw SQL query in Rails? sqlite sqlite

How to chain raw SQL queries in Rails OR how to return an ActiveRecord_Relation from a raw SQL query in Rails?


This is a way to get an ActiveRecord_Relation from raw_sql.
It works best if you actually have a model matching the fields you're trying to retrieve, but as you can see with test_attribute, any data will be loaded.

# Just an example query, any query should be okquery = <<-SQL  SELECT *, TRUE AS test_attribute  FROM users  WHERE sign_in_count < 10SQLrelation = User.select('*').from("(#{query}) AS users")relation.class# User::ActiveRecord_Relationrelation.first.test_attribute# true