简体   繁体   English

Rails中未定义的方法sort_by

[英]Undefined method sort_by in rails

I am facing issues while using sort or sort_by in my presenter and controller. 我在演示者和控制器中使用sort或sort_by时遇到问题。 It says undefined method 'sort_by' . 它说未定义的方法'sort_by'。

Does it belong to any particular class? 它属于任何特定类别吗? I have looked it up online but I am not able to find anything concrete. 我已经在网上查询过,但是找不到任何具体的东西。

Can any one shed light on this? 有人可以阐明这一点吗?

Here is my presenter code- 这是我的主持人代码-

def sortDocument()
    @list.sort do |a, b|
      (b.published_date <=> a.published_date) ||
      a.display_name <=> b.display_name
      @list
end

and alternatively 或者

def sortDocument()
  @list.sort_by!{ |m| m.published_date }
end

EDIT: 编辑:

Error message: 错误信息:

undefined method `sort_by!' for #<DocumentsBureauServices::DocumentList:0x007ff8250f8a28>

presenter- 主持人-

class DocumentPresenter

  def initialize(list)
    @list = list
    @list = sort_document()
  end
 def sortDocument()
    @list.sort do |a, b|
      (b.published_date <=> a.published_date) ||
      a.display_name <=> b.display_name
      @list
 end
end

and alternatively 或者

 def sortDocument()
      @list.sort_by!{ |m| m.published_date }
    end

Sorts enum using a set of keys generated by mapping the values in enum through the given block...to use sort_by ... 使用通过将枚举中的值映射到给定块中而生成的一组键对枚举进行排序...以使用sort_by ...

%w{ apple pear fig }.sort_by {|word| word.length}
              #=> ["fig", "pear", "apple"]

It looks like the DocumentsBureauServices::DocumentList doesn't include the enumerable module and doesn't have access to the sort_by method. 看起来DocumentsBureauServices::DocumentList不包含可枚举的模块,并且无法访问sort_by方法。

If indeed DocumentList is a decorator around an enumerable object then you could use a delegator. 如果确实DocumentList是可枚举对象周围的装饰器,则可以使用委托器。 ActiveSupport has a method dedicated to delegating: http://apidock.com/rails/Module/delegate ActiveSupport具有专用于委派的方法: http ://apidock.com/rails/Module/delegate

In your case, you could use it like this: 在您的情况下,可以这样使用它:

class DocumentsBureauServices::DocumentList

  delegate :sort_by!, to: :my_enumerable

  def initialize(enumerable)
    @my_enumerable = enumerable
  end

end

And then you could call sort_by! 然后您可以调用sort_by! on your list object like this: 在您的列表对象上,如下所示:

@list = DocumentsBureauServices::DocumentList.new(Document.all)
@list.sort_by! {|m| m.published_date}
#=> [#Document1, #Document2, etc]

If you want to know more about creating your own presenters, might I suggest the excellent RailsCast about it http://railscasts.com/episodes/287-presenters-from-scratch 如果您想了解更多有关创建自己的演示者的信息,我是否可以推荐出色的RailsCast http://railscasts.com/episodes/287-presenters-from-scratch

I personally use the Draper library for presenters: https://github.com/drapergem/draper which makes presenting ActiveRecord objects a breeze. 我个人将Draper库用于演示者: https : //github.com/drapergem/draper ,使ActiveRecord对象的演示变得轻而易举。 There's a RailsCast about it here: http://railscasts.com/episodes/286-draper 此处有一个RailsCast: http ://railscasts.com/episodes/286-draper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM