Rails Draper Gem为嵌套模型提出未定义的方法

时间:2015-02-08 21:34:50

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

我的应用程序中有各种相关模型。我已经设置了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映射是正确的,所以不可能。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

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

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

这也改善了整体查询时间,所以我对解决方案感到满意!