未定义的方法`__sort_option __' for:祖先:符号

时间:2015-02-28 20:01:34

标签: ruby-on-rails mongoid activeadmin

尝试在ActiveAdmin中查看树时,会抛出错误: undefined method 'sort_option' for :ancestry:Symbol

这不适用于组合:Mongoid,mongoid-ancestry和active_admin-sortable_tree 但适用于ActiveRecord,Ancestry和active_admin-sortable_tree

示例源代码位于:https://github.com/nahankid/community

违规代码段

def order_by(*spec)
  option(spec) do |options, query|
    spec.compact.each do |criterion|
      criterion.__sort_option__.each_pair do |field, direction|
        add_sort_option(options, field, direction)
      end

跟踪(不完整)

> ActionView::Template::Error (undefined method `__sort_option__' for
> :ancestry:Symbol):
>     1: insert_tag renderer_for(:index)   origin (2.1.1) lib/origin/optional.rb:173:in `block (2 levels) in order_by'   origin
> (2.1.1) lib/origin/optional.rb:172:in `each'   origin (2.1.1)
> lib/origin/optional.rb:172:in `block in order_by'   origin (2.1.1)
> lib/origin/optional.rb:314:in `block in option'   origin (2.1.1)
> lib/origin/optional.rb:312:in `tap'   origin (2.1.1)
> lib/origin/optional.rb:312:in `option'   origin (2.1.1)
> lib/origin/optional.rb:171:in `order_by'   active_admin-sortable_tree
> (0.2.0) lib/active_admin/views/index_as_sortable.rb:139:in `block (2
> levels) in build_nested_item'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:31:in `block in build_tag'  
> arbre (1.0.3) lib/arbre/context.rb:92:in `with_current_arbre_element' 
> arbre (1.0.3) lib/arbre/element/builder_methods.rb:49:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:26:in `build_tag'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:39:in `insert_tag'   arbre
> (1.0.3) lib/arbre/element/builder_methods.rb:14:in `ol'  
> active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:138:in `block in
> build_nested_item'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:31:in `block in build_tag'  
> arbre (1.0.3) lib/arbre/context.rb:92:in `with_current_arbre_element' 
> arbre (1.0.3) lib/arbre/element/builder_methods.rb:49:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:26:in `build_tag'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:39:in `insert_tag'   arbre
> (1.0.3) lib/arbre/element/builder_methods.rb:14:in `li'  
> active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:117:in `build_nested_item'
> active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:86:in `block (2 levels) in
> build_list'   active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:85:in `each'  
> active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:85:in `block in
> build_list'   arbre (1.0.3) lib/arbre/element/builder_methods.rb:31:in
> `block in build_tag'   arbre (1.0.3) lib/arbre/context.rb:92:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:49:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:26:in `build_tag'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:39:in `insert_tag'   arbre
> (1.0.3) lib/arbre/element/builder_methods.rb:14:in `ol'  
> active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:84:in `build_list'  
> active_admin-sortable_tree (0.2.0)
> lib/active_admin/views/index_as_sortable.rb:23:in `build'   arbre
> (1.0.3) lib/arbre/element/builder_methods.rb:30:in `block in
> build_tag'   arbre (1.0.3) lib/arbre/context.rb:92:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:49:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:26:in `build_tag'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:39:in `insert_tag'  
> /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/active_admin-2e5e954bc9b1/lib/active_admin/views/pages/index.rb:139:in
> `block (2 levels) in render_index'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:31:in `block in build_tag'  
> arbre (1.0.3) lib/arbre/context.rb:92:in `with_current_arbre_element' 
> arbre (1.0.3) lib/arbre/element/builder_methods.rb:49:in
> `with_current_arbre_element'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:26:in `build_tag'   arbre (1.0.3)
> lib/arbre/element/builder_methods.rb:39:in `insert_tag'   arbre
> (1.0.3) lib/arbre/element/builder_methods.rb:14:in `div'  
> /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/active_admin-2e5e954bc9b1/lib/active_admin/views/pages/index.rb:138:in
> `block in render_index'   ar

1 个答案:

答案 0 :(得分:0)

这里的罪犯是active_admin-sortable树。假设您使用的是ActiveRecord,并且不符合Mongoid的排序语法。

在Mongoid中,您必须指定排序的方向,1或-1表示升序或降序。这是因为如果没有提供方向,MongoDB的查询语法没有对排序方向的假设,这与SQL假定如果没有给出升序那样假设。

所以当宝石在这里调用order时:

https://github.com/zorab47/activeadmin-sortable-tree/blob/0b78ebffb2e4b702bde99c93fb29b6475504fefc/lib/active_admin/views/index_as_sortable.rb#L139

它没有提供方向。如果是这样的话:

item.send(options[:children_method]).order(options[:sorting_attribute] => :asc).each do |c|

然后它将兼容两者,至少仅在这种情况下。我不确定那个宝石会有什么其他问题。