Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:36:27+00:00 2026-05-20T09:36:27+00:00

I am trying to the get SQL from AREL, but it does not work

  • 0

I am trying to the get SQL from AREL, but it does not work in case I use average(:stars) :

This works:

Review.where("reviewed_user_id = ?", self.reviewed_user_id).to_sql
#=> "SELECT `reviews`.* FROM `reviews` WHERE (reviewed_user_id = 3)"

This causes NoMethodError:

Review.where("reviewed_user_id = ?", self.reviewed_user_id).average(:stars).to_sql
#=> undefined method `to_sql' for 3:Fixnum

So that means that to_sql is getting called on the result of the AREL instead of on the AREL object – but why?

How to get the generated SQL ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:36:27+00:00Added an answer on May 20, 2026 at 9:36 am

    The reason this is happening is because the average method is on ActiveRecord::Relation, not Arel, which forces the computation.

    m = Review.where('id = ?', 42).method(:average)
    #=> #<Method: ActiveRecord::Relation(ActiveRecord::Calculations)#average>
    m.source_location  # or m.__file__ if you're on a different version of Ruby
    #=> ["/Users/jtran/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.4/lib/active_record/relation/calculations.rb", 65]
    

    By checking out the internals of ActiveRecord::Calculations, you can derive how to get at the SQL that it uses.

    my_reviewed_user_id = 42
    relation = Review.where('reviewed_user_id = ?', my_reviewed_user_id)
    column = Arel::Attribute.new(Review.unscoped.table, :stars)
    relation.select_values = [column.average]
    relation.to_sql
    #=> "SELECT AVG(\"reviews\".\"stars\") AS avg_id FROM \"reviews\" WHERE (reviewed_user_id = 42)"
    

    Careful if you’re working at the console. ActiveRecord::Relation caches things so if you type the above into the console line by line, it will actually not work, because pretty-printing forces the relation. Separating the above by semicolons and no new lines, however, will work.

    Alternatively, you can use Arel directly, like so:

    my_reviewed_user_id = 42
    reviews = Arel::Table.new(:reviews)
    reviews.where(reviews[:reviewed_user_id].eq(my_reviewed_user_id)).project(reviews[:stars].average).to_sql
    #=> "SELECT AVG(\"reviews\".\"stars\") AS avg_id FROM \"reviews\" WHERE \"users\".\"reviewed_user_id\" = 42"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to get the following sql to work in mysql but
I am trying to get the sql query below to work but I am
I am trying to get this SQL query to grab 1 more thing from
Ive been trying to get this sql statement to work properly through excel vba.
I'm trying to get this LINQ to work but fails with the error. Cannot
I'm trying to get SQL Server to order by a column from a nested
I get the following error from the SQL Script I am trying to run:
I'm trying to get the following SQL statement to work: UPDATE myschema.tableA update_tableA SET
I'm trying to get the IsAdmin Value with SQL query(This query return 1 line
I am trying to get data from my sql server data table to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.