I have a Profile that can be published. A profile belongs_to :user and has_many :ratings.
A User has_one :profile, and has_many :ratings.
A Rating belongs_to :profile && belongs_to :user.
These are the schemas for the above models:
Profile.rb:
# == Schema Information
#
# Table name: profiles
#
# id :integer not null, primary key
# first_name :string
# last_name :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
User.rb:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
# first_name :string
# last_name :string
Rating.rb
# == Schema Information
#
# Table name: ratings
#
# id :integer not null, primary key
# speed :integer default(0)
# passing :integer default(0)
# tackling :integer default(0)
# dribbling :integer default(0)
# profile_id :integer
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
What I want to do is to find all the profiles, ranked by 1 rating attribute....e.g. all published profiles ranked by passing (highest to lowest).
I tried something like this:
Profile.published.where(id: coach.ratings.order(passing: :desc).pluck(:profile_id))
But that doesn't always give me the profiles in the order I expect.
So how do I do this query that allows me to get these profiles ranked by all of those ratings accordingly?
Edit 1
Please Note The key thing here is that I need to find the ratings on a profile left by a specific user.
In my query above, coach = User.find(7).
So each User leaves a bunch of ratings on many profiles.
What I want to do is filter all the profiles, that have a specific rating (say speed) and order those profiles, by the speed rating from highest to lowest (but this is user specific).