Ruby on rails 按和排序对象有许多关联
我有两个目标:Ruby on rails 按和排序对象有许多关联,ruby-on-rails,Ruby On Rails,我有两个目标: class Person < ActiveRecord::Base has_many :tests end class Test <ActiveRecord::Base belongs_to :person end 一旦我有了这个问题,我该如何将其返回到玩家列表中进行排序?这是一个老问题,但最近我试图找到类似问题的解决方案: 在rails 3中,它只是 class Person < ActiveRecord::Base has_many :t
class Person < ActiveRecord::Base
has_many :tests
end
class Test <ActiveRecord::Base
belongs_to :person
end
一旦我有了这个问题,我该如何将其返回到玩家列表中进行排序?这是一个老问题,但最近我试图找到类似问题的解决方案: 在rails 3中,它只是
class Person < ActiveRecord::Base
has_many :tests
scope :order_by_score, lambda {|direction| joins(:tests).group(:person_id).order("sum(score) #{direction}")
end
# when Person also has attribute 'score', it should be .order("sum('tests.score') #{direction}")
class-Person
老问题,但最近我试图找到类似问题的解决方案:
在rails 3中,它只是
class Person < ActiveRecord::Base
has_many :tests
scope :order_by_score, lambda {|direction| joins(:tests).group(:person_id).order("sum(score) #{direction}")
end
# when Person also has attribute 'score', it should be .order("sum('tests.score') #{direction}")
class-Person