祖先宝石'未定义的方法'错误 [英] Ancestry Gem 'undefined method' error

查看:136
本文介绍了祖先宝石'未定义的方法'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ancestry Gem,我得到了一个未定义的方法,用以下代码#排列#< Array:0x007f8d58e58700> 错误:

 <%= @ shipmgr_carriers.arrange(:order => name)%> 

然而,当我输出 @shipmgr_carriers 我得到一个带有祖先的数组:

  [#< Shipmgr :: Carrier id:9,name:testing,status :nil,created_at:2012-01-16 22:44:28,updated_at:2012-01-16 22:44:28,ancestry:nil> ;,#< Shipmgr :: Carrier id:10,name :test,status:nil,created_at:2012-01-16 22:44:28,updated_at:2012-01-16 22:44:28,血统:9>] 

任何人都可以告诉我为什么我无法调用数组变量的.arrange方法吗?

解决方案

我没有使用过Ancestry,但它看起来像它将 arrange 方法添加到ActiveRecord ::基础。它不会将此方法添加到数组中,因此您不应该指望它在数组上工作。



我假设您声明 @shipmgr_carriers 就像这样:

  class SomeController< ActionController :: Base 
def some_action
@shimgr_carriers = SomeModel.find(...)。运营商
结束
结束

试试这个:

  def some_action 
@shipmgr_carriers = SomeModel.find(...)。运营商

@shipmgr_carriers_arranged = @ shipmgr_carriers.arrange(...)
结束

我的直觉是,当 @shipmgr_carriers 到达您的视图时,查询已经运行,将它转换为一个结果数组而不是一个关系。

这是逻辑,应该可以在控制器中完成,所以将它移出视图是一件好事无论什么想法。


I am using the Ancestry Gem and I am getting an undefined method "arrange" for #<Array:0x007f8d58e58700> error with the following code:

<%= @shipmgr_carriers.arrange(:order => :name) %>

However, When I just output @shipmgr_carriers I get an array with ancestry:

[#<Shipmgr::Carrier id: 9, name: "testing", status: nil, created_at: "2012-01-16 22:44:28", updated_at: "2012-01-16 22:44:28", ancestry: nil>, #<Shipmgr::Carrier id: 10, name: "test", status: nil, created_at: "2012-01-16 22:44:28", updated_at: "2012-01-16 22:44:28", ancestry: "9">] 

Can anyone tell me why I am unable to call the .arrange method on the array variable?

解决方案

I haven't used Ancestry but it looks like it adds the arrange method to ActiveRecord::Base. It doesn't add this method to Array and so you shouldn't expect it to work on an array.

I'm assuming you're declaring @shipmgr_carriers in your controller something like this:

class SomeController < ActionController::Base
  def some_action
    @shimgr_carriers = SomeModel.find(...).carriers
  end
end

Try this instead:

def some_action
  @shipmgr_carriers = SomeModel.find(...).carriers

  @shipmgr_carriers_arranged = @shipmgr_carriers.arrange(...)
end

My hunch is that by the time @shipmgr_carriers gets to your view the query has already been run, transforming it into an array of results instead of a relation.

This is logic that should probably be done in the controller anyway, so moving it out of the view is a good idea regardless.

这篇关于祖先宝石'未定义的方法'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆