このような会社のモデルがあります
class Company < ActiveRecord::Base
has_many :values
end
次に、値モデルは次のようになります
class Value < ActiveRecord::Base
belongs_to :company
default_scope order: 'created_at ASC'
end
会社が最新の値に基づいて並べ替えられるように、会社の既定の並べ替え順序を作成したいと考えています。最新の値を持つ会社が最初になります。このようなもの:
default_scope order: 'companies.values.last.created_at DESC'
しかし、それを会社のモデルに入れると、次のエラーが発生します。
SQLite3::SQLException: near "values": syntax error: SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? ORDER BY companies.values.last.calculated_at DESC LIMIT 1