你能做一个:找到后在红宝石中订购吗?

时间:2022-09-28 14:39:42

Is it possible in ruby/rails to order a result set after doing a find? For example, is it possible to do something like this

在执行查找后,ruby / rails是否可以对结果集进行排序?例如,是否可以做这样的事情

Warning: Does not work

警告:不起作用

if type == 1
  @items = mycompany.items
else
  @items = myhome.items
end

@items = @items :order => "created_at"

I would assume something like this should be possible, but I'm still very new to RoR and can't seem to find anything on google.

我会假设这样的事情应该是可能的,但我仍然是RoR的新手,似乎无法在谷歌上找到任何东西。

3 个解决方案

#1


I don't know why everyone's all "No you can't" when it's possible in named scopes.

我不知道为什么每个人都在“不能你不能”的时候在命名的范围内。

For example, define as part of ActiveRecord::Base:

例如,定义为ActiveRecord :: Base的一部分:

named_scope :by_created_at, :order_by => 'created_at'

This allows you to convert a simple relationship to an ordered one before it is actually retrieved:

这允许您在实际检索之前将简单关系转换为有序关系:

@items = @items.by_created_at

As a note it's not a good idea to include scopes which have overlapping :order definitions as this causes them to be appended instead of over-riding each in turn.

作为注释,包含具有重叠的范围不是一个好主意:订单定义因为这会导致它们被附加而不是依次覆盖每个范围。

However, in your example it's not too much of a stretch to imagine refactoring it as the following:

但是,在您的示例中,想象重构如下所示并不是太过分:

reference =
  case (type)
  when 1
     mycompany
  else
    myhome
  end

@items = reference.items.all(:order_by => 'created_at')

#2


You should be able to do one of these:

你应该可以做其中一个:

Sort when accessed:

访问时排序:

if type == 1
  @items = mycompany.items(:order => 'created_at')
else
  @items = myhome.items(:order => 'created_at')
end

Sort after accessing:

访问后排序:

@items.sort{|a,b| a.created_at <=> b.created_at}

#3


You could also use the Symbol to Proc shorthand available in rails, along with Enumerable#sort_by to simplify your sort statement, if you want to sort after collecting ActiveRecord objects:

如果要在收集ActiveRecord对象后进行排序,还可以使用rails中可用的Symbol to Proc简写以及Enumerable#sort_by来简化排序语句:

@items.sort_by(&:created_at)
# is the same as @items.sort{ |a, b| a.created_at <=> b.created_at }

#1


I don't know why everyone's all "No you can't" when it's possible in named scopes.

我不知道为什么每个人都在“不能你不能”的时候在命名的范围内。

For example, define as part of ActiveRecord::Base:

例如,定义为ActiveRecord :: Base的一部分:

named_scope :by_created_at, :order_by => 'created_at'

This allows you to convert a simple relationship to an ordered one before it is actually retrieved:

这允许您在实际检索之前将简单关系转换为有序关系:

@items = @items.by_created_at

As a note it's not a good idea to include scopes which have overlapping :order definitions as this causes them to be appended instead of over-riding each in turn.

作为注释,包含具有重叠的范围不是一个好主意:订单定义因为这会导致它们被附加而不是依次覆盖每个范围。

However, in your example it's not too much of a stretch to imagine refactoring it as the following:

但是,在您的示例中,想象重构如下所示并不是太过分:

reference =
  case (type)
  when 1
     mycompany
  else
    myhome
  end

@items = reference.items.all(:order_by => 'created_at')

#2


You should be able to do one of these:

你应该可以做其中一个:

Sort when accessed:

访问时排序:

if type == 1
  @items = mycompany.items(:order => 'created_at')
else
  @items = myhome.items(:order => 'created_at')
end

Sort after accessing:

访问后排序:

@items.sort{|a,b| a.created_at <=> b.created_at}

#3


You could also use the Symbol to Proc shorthand available in rails, along with Enumerable#sort_by to simplify your sort statement, if you want to sort after collecting ActiveRecord objects:

如果要在收集ActiveRecord对象后进行排序,还可以使用rails中可用的Symbol to Proc简写以及Enumerable#sort_by来简化排序语句:

@items.sort_by(&:created_at)
# is the same as @items.sort{ |a, b| a.created_at <=> b.created_at }