如何对特定用户的关联进行排序? [英] How do I sort on an association of a specific user?
问题描述
我有一个个人资料
,可以发布
。 配置文件
属于:user
和 has_many:ratings
。 / p>
A 用户has_one:profile
和 has_many:ratings
。
A 评级归属于:profile&&这些都是上述模型的模式:
Profile.rb
:
#==架构信息
#
#表名:配置文件
#
#id:整数不为null,主键
#first_name:string
#last_name:string
#created_at :datetime不为空
#updated_at:datetime不为空
#user_id:整数
User.rb
:
#==架构信息
#
#表名:用户
#
#id:整数不为null,主键
#email:string default(),不为null
# created_at:datetime不为空
#updated_at:datetime不为空
#first_name:string
#last_name :string
Rating.rb
#==模式信息
#
#表名:等级
#
# id:integer不为null,主键
#速度:integer default(0)
#传递:integer default(0)
#处理:integer default(0)
#盘带:integer default(0)
#profile_id:integer
#user_id:integer
#created_at:datetime not null
#Updated_at:datetime not null
#
我要做的是找到所有配置文件,以1个评级排名
属性....例如所有发布的个人资料均按通过
(从高到低)进行排名。
我尝试过这样的事情:
Profile.published.where(id:coach.ratings.order(passing::desc).pluck(:profile_id))
但这并不总是按我期望的顺序为我提供配置文件。
那么我该如何进行查询以使这些配置文件按所有这些评分进行相应排名?
编辑1
请注意,这里的关键是我需要找到特定用户留下的个人资料上的评分。
在上面的查询中, coach = User.find(7)
。
因此,每个用户
在许多个人资料上都留下了一堆
。评分
我要做的是过滤所有个人资料
,特定等级(例如 speed
),然后按照速度等级从最高到最低对这些配置文件进行排序(但这是用户特定的)。
使用联接:
Profile.published
.joins(:ratings)
.where(ratings:{user_id:coach.id})
.order('ratings.passing')
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).
Use a join:
Profile.published
.joins(:ratings)
.where(ratings: { user_id: coach.id } )
.order('ratings.passing')
这篇关于如何对特定用户的关联进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!