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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:52:20+00:00 2026-05-23T06:52:20+00:00

Currently i have two models: Rate and Item Rate is a model for voting,

  • 0

Currently i have two models: Rate and Item

Rate is a model for voting, and there is votes and player_id

Rate has_many :votes
Vote belongs_to :rate

Also, for Item model, currently i have a scope like:

scope :pop, proc { order('votes.votes DESC') }

to sort all Items by vote.

The question is: i need to collect items sorted (Item.all.pop) AND by player_id
Something like: Item.all.pop(player_id)

How it could be done?

Update:

rate.rb

class Rate < ActiveRecord::Base
  belongs_to :player
  belongs_to :item
end

item.rb

class Item < ActiveRecord::Base
  has_many :rates
  scope :pop, proc { order('rates.votes DESC') }
end
  • 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-23T06:52:20+00:00Added an answer on May 23, 2026 at 6:52 am

    UPDATE: (based on post update and comment clarifications)

    You probably want something like this:

    scope :pop, order('stats.votes DESC')
    
    scope :for_player, lambda{|player| joins(:rates).where(:player_id => player.id)}
    

    Then you should be able to call Item.for_player(@my_player).pop.

    Disclaimer: I’m not great at building active record queries off the top of my head, so the above may need some tweaking…


    (Original answer)

    You probably want something like this:

    scope :pop, order('stats.votes DESC')
    
    scope :by_player, group('player_id')
    

    Then, you should be able to do Item.by_player.pop and get what you want. (You can check in the console with Item.by_player.pop.to_sql to see if the expected SQL query is generated.)

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

Sidebar

Related Questions

I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a
I have two models, users and themes, that I'm currently joining in a HABTM
Currently I have two tables in my database Encounters and Referrals : There is
I currently have a Rails 3.0.10 app and I made two models with simple
I'm writing a Rails app in which I have two models: a Machine model
How to add an computed column to the model class. I have two models
Suppose you have two models, User and City, joined by a third model CityPermission:
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have two models, Item and Link. Each link is going to join two
I have two basic models, Story and Category: class Category(models.Model): title = models.CharField(max_length=50) slug

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.