【问题标题】:Difficult Child <> Parent <> Child (sibling) SQL Query with Rails & Active Record困难的子 <> 父 <> 子(兄弟)SQL 查询与 Rails 和 Active Record
【发布时间】:2016-05-11 06:16:56
【问题描述】:

我有一个trailers 表。预告片属于电影。电影 has_many 通知。预告片 has_many 通知:through 电影。

我想按预告片的通知数量对预告片进行排序。我尝试了几个不同的查询(都没有奏效),我觉得我过于复杂了。

有人可以帮我开始吗?

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    这就是我最终登陆的地方:

    Trailer.joins(:movie => :notifications).
              select('trailers.*, COUNT(notifications.id) AS notifications_count').
              joins(:movie).group('trailers.id').
              order('notifications_count DESC')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2012-04-17
      • 2016-03-15
      • 1970-01-01
      相关资源
      最近更新 更多