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

  • Home
  • SEARCH
  • 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 7613501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:10:16+00:00 2026-05-31T02:10:16+00:00

I have two models that are associated. I have an Artist model and a

  • 0

I have two models that are associated. I have an Artist model and a Review model.

class Artist
  include DataMapper::Resource

  property :id, Serial
  property :name, String

  has n, :reviews

end

class Review
  include DataMapper::Resource

  property :id, Serial
  property :rating, Integer
  property :body, String

  belongs_to :artist
end

My goal is to retrieve an array of artists ordered by the average rating of the reviews associated with them. I’m not sure if there is any one line solution to this, but my current method is to create an array with the id’s of the artists sorted by average rating. The details of how I obtain this array is not important for this question but let’s say I now have an array.

ids_sorted_by_rating = [4,5,23,9,2,48,17,....]

I then retrieve the artists.

artists = Artist.all(:id => ids_sorted_by_rating)

The problem is that datamapper orders by :id unless you specify another option. So after all my hard work to get ids_sorted_by_rating, I’m still left with a list of artists just sorted by :id.

Any solution that gives me a list of artists ordered by the average rating of the associated reviews would be MUCH appreciated. Bonus points if I don’t need to hit the database a million times to get that list. 🙂

Thanks and if you need more info just ask and I’ll provide!

  • 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-31T02:10:17+00:00Added an answer on May 31, 2026 at 2:10 am

    AFAIK there is no easy way to do sorting by aggregation in DataMapper (the way to go to do it in single query). You can use an app-side reordering of results (unless your datasets are too big for this):

    hash = artists.index_by(&:id)
    # if you do not have index_by - use a artists.inject({}){|h,a| h.merge(a.id => a)} 
    ids_sorted_by_rating.map{|id| hash[id]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models, Tweets and TweetsLocations, associated to each other with: class Tweets
I have two models, Room and Image . Image is a generic model that
I have a two models: class Category(models.Model): pass class Item(models.Model): cat = models.ForeignKey(Category) I
I have a Rails simple application that has two main models. A person model
If I have two models that are very different, is it defeating the purpose
If I have two models that are guaranteed to have a one-to-one correspondence, i.e.
I have a two models that are similar, but not exactly the same. Here's
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now
In my PHP project I have two different models that have a function with

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.