Вопрос

Relationship User has_many :kits

User model and named scope:

  scope :top5_users,
    joins(:kits).
    select("users.*, count(kits.id) AS kits_count").
    group("users.id, kits.id").
    order("kits_count DESC").
    limit(5)

Getting PG::Error: ERROR: ORDER BY "kits_count" is ambiguous

Это было полезно?

Решение

That error message implies you have a column in users called kits_count, possibly maintained by a callback. If so you could more easily just:

scope :top5_users,
    order("kits_count DESC").
    limit(5)

If not ...

Support for column aliases is mixed among RDBMS vendors, and you could probably use:

order("count(kits.id) desc")

If you knew the column number you could:

scope :top5_users,
    joins(:kits).
    select("count(kits.id) AS kits_count, users.*").
    group("users.id").
    order("1 DESC").
    limit(5)

Take the kits.id out of the group by or you'll get 1 for every count.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top