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 4596950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:11:17+00:00 2026-05-21T23:11:17+00:00

I always wondered how to query and get results that doesn’t fit in a

  • 0

I always wondered how to query and get results that doesn’t fit in a model. Similar how it’s done using LINQ and projecting into anonymous objects.

So here’s the simple schema:

# Product.rb
class Product < ActiveRecord::Base
   has_many :product_views

   # attributes: id, name, description, created_at, updated_at
end

# ProductView.rb
class ProductView < ActiveRecord::Base
   belongs_to :product

   # attributes: id, product_id, request_ip, created_at, updated_at
end

Basically I need to get a list of Products (preferably just id and name) along with the count of views it had. Obviously ordered by view count desc.

This is the SQL I want to get:

select 
    p.id,
    p.name,    
    count(pv.product_id) as views
from 
    product_views pv
inner join
    products p on pv.product_id = p.id
group by
    pv.product_id
order by
    count(product_id) desc

I tried the following and similar, but I’m getting ProductView objects, and I would like to get just an array or whatever.

ProductView.includes(:product)
           .group('product_id')
           .select("products.id, products.name, count(product_id)")

This kind of thing are trivial using plain SQL or LINQ, but I find myself stucked with this kind of queries in Rails. Maybe I’m not thinking in the famous ‘rails way’, maybe I’m missing something obvious.

So how do you do this kind of queries in Rails 3, and specifically this one? Any suggestions to improve the way I’m doing this are welcome.

Thank you

  • 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-21T23:11:18+00:00Added an answer on May 21, 2026 at 11:11 pm

    Within a class method in the Product class:

    Product.includes(:product_views).all.map { |p| [p.id, p.name, p.product_views.size] }
    

    Then sort it however you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always wondered why there's an equals keyword in linq joins rather than using
I always wondered how to document a method that overrides a message from a
I've always wondered is there a better way that I should be writing some
I've always wondered. I know that compilers convert the code you write into binaries
I've been using the Krypton Suite/Toolkit to great benefit, but I've always wondered why
I've always wondered why the JVM doesn't tell you which pointer (or more precisely,
I always wondered how all the programs that has a progress bar can know
I've always wondered how I could get away with this: int main(int argc, char
List<String> works with or without but List<int> doesn't. I've always wondered about this.
I've always wondered, because I see that they send 404 or 500 or whatever

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.