【问题标题】:Rails Draper Gem raising undefined method for Nested ModelRails Draper Gem 提高嵌套模型的未定义方法
【发布时间】:2015-02-08 21:34:49
【问题描述】:

我的应用程序中有各种关联模型。我已经设置 Draper 在父类初始化时装饰关联。

我有以下课程:

Customer (has_many :accounts)
Account  (belongs_to :customer && has_many :facilities && has_one :contact)
Contact  (belongs_to :account)
Facility (belongs_to :account)

我如下启动一个装饰器:

@customer = Customer.find_by(:token => params[:token]).decorate

在我的 account_decorator 中添加了以下内容:

decorates_association :contact
decorates_association :facilities

...在我的客户装饰器中,我有:

decorates_association :accounts

我遇到的问题是,facility_decorator 中定义的任何方法都会显示未定义的方法异常。 contact_decorator 工作得非常好。我已经确定我的 AR 映射是正确的,所以不可能这样。

有什么建议吗?

【问题讨论】:

  • 也许Facility 装饰器存在不相关的问题。您是否尝试过删除关联并仅实例化修饰的Facility,只是为了消除它?
  • @Ninjarabbi ,我已经测试了它自己的实例化设施并在其上使用 .decorate 。这在这种情况下似乎确实有效,因为我可以访问 FacilityDecorator 方法
  • 从表面上看,contactfacility 之间没有区别。如果不查看所有代码,很难弄清楚这一点。我会尝试通过将accountcontact 之间的关系更改为has_many 来消除因素,看看它是否仍然有效。然后我会尝试在这两个模型之间复制部分代码,直到我发现是什么让 'contact' 工作而 facility 不工作。

标签: ruby-on-rails ruby-on-rails-4 draper


【解决方案1】:

我通过 eager_loading 关联解决了这个问题。

@customer = Customer.eager_load({ :account => [:facilities]}).find_by(:token => params[:token]).decorate

这也缩短了整体查询时间,所以我对这个解决方案很满意!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2014-08-26
    • 1970-01-01
    相关资源
    最近更新 更多