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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:13:32+00:00 2026-06-04T02:13:32+00:00

My simplified rails app looks like this # chapter.rb has_many :sections has_many :videos, through:

  • 0

My simplified rails app looks like this

# chapter.rb
has_many :sections
has_many :videos, through: :sections

# section.rb
belongs_to :chapter
has_many :videos

# video.rb
belongs_to :section
has_many :votes

# vote.rb
belongs_to :video

What I want to do is find the top 5 currently popular videos for a given chapter, which would mean ordering the videos by the number of votes it has received within the last month (and limiting to 5 results, obviously).

I wanted to write a popular_videos method in my Chapter model, and I think this requires a find_by_sql query, right? But I don’t know enough sql to write this query.

The votes table has a created_at column, and I’m using postgres for my database. Any help is greatly appreciated.

  • 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-06-04T02:13:33+00:00Added an answer on June 4, 2026 at 2:13 am

    I don’t know anything about rails, but guessing at the table structures from what’s in the question, I would think SQL like the following would be one way to provide what you want:

    SELECT video.id, video.name, count(*) as vote_count
      FROM video JOIN vote ON (video.id = vote.video_id)
      WHERE vote.date > (current_date - interval '1 month')
      GROUP BY video.id, video.name
      ORDER BY vote_count DESC
      LIMIT 5;
    

    If you’re running PostgreSQL 9.1 or later, you can probably omit video.name from the GROUP BY list.

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

Sidebar

Related Questions

I'm trying to use capybara with rspec to test my rails app. I've simplified
I have a Rails app with the following (simplified) models: # member.rb class Member
Following simplified code snippet: #include <QtGui> int main(int argc, char **argv) { QApplication app(argc,
This is a simplified and explained version of my code: var ptext=parr.find('div.ptext'); //Text Container
Having this (simplified) XML: <?xml version=1.0 encoding=UTF-8?> <kml> <Document> <Placemark> <name>Poly 1</name> <Polygon> <coordinates>
I have a rails app which requires users to verify that they own a
In creating a ruby on rails / jquery app, there's a part of a
I am designing a Rails app that takes in requests, uses data within the
I am working on a small rails app and have a problem with ruby's
I am developing a small app in Rails 3. On one page I am

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.