Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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
Mysql ActiveRecord按特定关系排序';s值_Mysql_Sql_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Mysql ActiveRecord按特定关系排序';s值

Mysql ActiveRecord按特定关系排序';s值,mysql,sql,ruby-on-rails,ruby,activerecord,Mysql,Sql,Ruby On Rails,Ruby,Activerecord,所以Foo.first.bar.last.value=>3 我有一堆:foo,每个都有一堆:bar,但我只关心最后一个:bar 我可以根据最后的:条值对我的:foos进行排序吗?酒吧需要检查镜吗 例如 Foo.first.bars => [{key: 'a', value: 1}, {key: 'b', value: 2}, {key: 'c', value: 3}] class Foo(ord){includes(:foos).order(“foos.value{ord.upcase}

所以<代码>Foo.first.bar.last.value=>3

我有一堆:foo,每个都有一堆:bar,但我只关心最后一个:bar

我可以根据最后的:条
值对我的:foos进行排序吗?酒吧需要检查镜吗

例如

Foo.first.bars => [{key: 'a', value: 1}, {key: 'b', value: 2}, {key: 'c', value: 3}]
class Foo(ord){includes(:foos).order(“foos.value{ord.upcase}”)}
结束
类栏
谢谢,@Doon!如果你想复制并粘贴到你自己的答案中,我很乐意给你信用。你们知道有并没有办法在酒吧里调用一个命名的作用域?比如:

class Foo < ActiveRecord::Base
  has_many :bar
  scope :latest_foo_value, ->(ord) { includes(:foos).order("foos.value #{ord.upcase}") }
end

class Bar < ActiveRecord::Base
  belongs_to :foo
  default_scope order: 'created_at DESC'
end
class Foo(ord){includes(:foos).order(“foos.current.value{ord.upcase}”)}
结束
类栏{order('created_at DESC')}
结束
那不行<代码>Mysql2::错误:“order子句”中的未知列“foos.current.value”


干杯

不清楚你想做什么。我有一张桌子:foos。我希望能够根据each:foo的last:bar的值对它们进行排序。我肯定示波器会在某个地方工作,但我还没弄明白。我可以通过使用可枚举的
Foo.all.sort by(&:some\u method)
其中
some\u method
正在计算最后一个:条的
值。这就清楚了吗?我的问题是,enumerable很慢…像这样有帮助吗--
Foo.includes(:bars).order('bars.last.value')
class Foo < ActiveRecord::Base
  has_many :bar
  scope :latest_foo_value, ->(ord) { includes(:foos).order("foos.value #{ord.upcase}") }
end

class Bar < ActiveRecord::Base
  belongs_to :foo
  default_scope order: 'created_at DESC'
end
class Foo < ActiveRecord::Base
  has_many :bar
  scope :latest_foo_value, ->(ord) { includes(:foos).order("foos.current.value #{ord.upcase}") }
end

class Bar < ActiveRecord::Base
  belongs_to :foo
  scope :current, -> { order('created_at DESC') }
end