我试图在一个应用程序中显示一些流行的排序规则,还显示了“喜欢”的数量(协作--“状态”等于2的关联),以及当前用户是否喜欢它(根据协作中的状态--请求用户的关联)。
我有这样的模型:
Collab:
- has_many :collaborations
Collaboration:
- belongs_to :collab
- belongs_to :user
User:
- has_many :collaborations作为以下查询的结果,我需要的是:
一个由10个collab对象组成的数组,其中collab对象包含:
PS:用户只能一次喜欢collab,所以只有一次协作--每个用户对collab的关联。
这就是我现在所拥有的。这给了我十条校对和联想的计数。我不确定如何只包含由提供的user_id提供的协作/协作属性。
popular_collabs = Collab
.left_outer_joins(:collaborations, :reports)
.select("collabs.*, SUM(CASE WHEN collaborations.status = 2 THEN 1 ELSE 0 END) as like_count")
.where(reports: {id: nil})
.where("collabs.deadline >= ?", Date.today)
.group(:id)
.order('like_count DESC')
.limit(10)发布于 2019-03-04 16:08:33
Collab:
- has_many :collaborations
- has_many :users, :through => :collaborations
Collaboration:
- belongs_to :collab
- belongs_to :user
User:
- has_many :collaborations
- has_many :collabs, :through => :collaborations(如何只包含由提供的user_id提供的协作/协作-属性)
popular_collabs = Collab
.left_outer_joins(:collaborations, :users, :reports)
.select("collabs.*, SUM(CASE WHEN collaborations.status = 2 THEN 1 ELSE 0 END) as like_count")
.where(users: {id: params[:user_id]})
.where(reports: {id: nil})
.where("collabs.deadline >= ?", Date.today)
.group(:id)
.order('like_count DESC')
.limit(10)或
popular_collabs = Collab
.left_outer_joins(:collaborations, :reports)
.select("collabs.*, SUM(CASE WHEN collaborations.status = 2 THEN 1 ELSE 0 END) as like_count")
.where("collaborations.user_id = ?", params[:user_id])
.where(reports: {id: nil})
.where("collabs.deadline >= ?", Date.today)
.group(:id)
.order('like_count DESC')
.limit(10)https://stackoverflow.com/questions/54986338
复制相似问题