0

私は2つのモデルを持っています:

class Country
  has_many :competitions
end

class Competition
  belongs_to :country
end

競技クラスには位置属性があります。管理者は、競技を順位別に並べ替えることができます。競争の最小の位置で国を並べ替えたいです。国との大会にも参加したいです。どうすればそれを達成できますか?

次のような出力が必要です。

X Country: (is at first order because Xcomp1's position is 1)
  Xcomp1 (position: 1)
  Xcomp2 (position: 12)

A Country: 
  Acomp1 (position:2)
  Acomp2 (position:3)

Z Country: (is at last position because minimum position of its competitions are higher than other ones)
  Zcomp1 (position:5)
4

1 に答える 1

1

私はあなたがしなければならないと思う:

Country.order("(select min(position) from competitions where competitions.country_id = countries.id) asc")

その構文が異なる RDBMS 間でどのように維持されるかはわかりません - PostgreSQL と Oracle でうまくいくはずです

于 2013-08-15T14:05:01.410 に答える