Method: ActiveRecord::Relation#to_sql

Defined in:
activerecord/lib/active_record/relation.rb

#to_sqlObject

Returns sql statement for the relation.

User.where(name: 'Oscar').to_sql
# SELECT "users".* FROM "users"  WHERE "users"."name" = 'Oscar'


1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'activerecord/lib/active_record/relation.rb', line 1210

def to_sql
  @to_sql ||= if eager_loading?
    apply_join_dependency do |relation, join_dependency|
      relation = join_dependency.apply_column_aliases(relation)
      relation.to_sql
    end
  else
    model.with_connection do |conn|
      conn.unprepared_statement { conn.to_sql(arel) }
    end
  end
end