Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 按和排序对象有许多关联_Ruby On Rails - Fatal编程技术网

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