【问题标题】:Calling an ActiveRecord association method from an array in Rails 3.2从 Rails 3.2 中的数组调用 ActiveRecord 关联方法
【发布时间】:2012-06-20 03:50:36
【问题描述】:

目标:生成一个 Excel 文档,其中包含来自 3 个关联模型的信息,该模型与我的 HTML 表格中的信息相似。 to_xls gem 需要它作为数组列表。

https://github.com/splendeo/to_xls

期望的输出:

(working for both)      (working for both)    (working in HTML, not in Excel)
territory.branch.name    territory.zip    territory.mailedcounts.maximum(:maileddate)
My Branch                90210            2012-05-01
My Branch                90211            2012-05-03
My Branch                90212            

一个分支有许多区域。 一个区域有许多 Mailedcounts。

我可以通过 show.html.erb 的内置 ActiveRecord 方法在我的视图中显示正确的数据

<% for territory in @territories %>
<tr>
  <td><%= territory.branch.name %></td>      
  <td><%= territory.zip %></td>
  <td><%= territory.mailedcounts.maximum(:maileddate) %></td>
</tr>
<% end >

这是我目前正确导出的内容

class BranchesController < ApplicationController
.
.
.
 def show
  @branch = Branch.find(params[:id])
  @territories = @branch.territories

  respond_to do |format|
    format.html
    format.xls { 
      send_data @territories.to_xls(:columns => [ { :branch => :name }, :zip ] )
    }
 end
end

这给了我可以正常工作的区域.branch.name 和区域.zip。从领土开始,我不知道如何获得我的邮寄计数信息。

【问题讨论】:

    标签: ruby-on-rails arrays excel activerecord associations


    【解决方案1】:

    使用自定义范围应该可以解决这个问题。

    class Territory < ActiveRecord::Base
      scope :mailed_counts_max_date, lambda {
        mailcounts.maximum(:maileddate)  
      }
    end
    

    然后在控制器中:

    class BranchesController < ApplicationController
     def show
      @branch = Branch.find(params[:id])
      @territories = @branch.territories
    
      respond_to do |format|
        format.html
        format.xls { 
          send_data @territories.to_xls(:columns => [ { :branch => :name }, :zip, :mailed_counts_max_date ] )
        }
     end
    end
    

    【讨论】:

    • 非常好。我可以使用它从我的控制器中获取逻辑并查看。 (像一个文明人。)现在遇到一个愚蠢的错误,明天再试一次。
    • 我注意到你有“mailedcounts”和“mailcounts”的混合......可能是你的错误的原因?
    • 感谢克里斯的帮助,你让我找到了正确的方向。通过调用内置的 ActiveRecord 关联方法来创建范围的语法给了我一个没有定义方法的错误。不确定这是设计使然还是我搞砸了。看起来 Territory 模型中的作用域或类是阻力最小的路径。
    【解决方案2】:

    你试过了吗(完全未经测试)

    format.xls {
      # essentially replicate what the view does
      arr = []
      for territory in @territories
        arr << [territory.branch.name, territory.zip,  territory.mailedcounts.maximum(:maileddate)] 
      end
      send_data arr.to_xls
    }
    

    如果它(gem?)需要一个数组列表,那么使用 ActiveRecord 并没有什么神圣不可侵犯的...

    【讨论】:

    • 再次感谢您的回复。我不能完全让它发挥作用,这令人失望,因为它会给我带来额外的灵活性。每次我接近 to_xls gem 深处的东西时都会抱怨。
    【解决方案3】:

    这是为我完成的解决方案。 (经过比应有的时间多得多的尝试。)

    诀窍是在 Mailedcount 模型中定义一个类,而不是在 Territory 模型中。

    class Mailedcount < ActiveRecord::Base
    .
    .
    .
      belongs_to :branch
      belongs_to :territory
    
      class << self
        def max_maileddate
          maximum('maileddate')
        end
      end
    end
    

    回到控制器,我现在可以调用那个方法了。

    class BranchesController < ApplicationController
    .
    .
    .
     def show
      @branch = Branch.find(params[:id])
      @territories = @branch.territories
    
      respond_to do |format|
      format.html
      format.xls { 
        send_data @territories.to_xls(:columns => [ { :branch => :name }, :zip,
                            { :mailedcounts => :max_maileddate } ] )
      }
        end
      end
    

    如果不从本质上复制与另一个联接的关系,我就无法获得在 Territory 模型中工作的范围或方法。

    【讨论】:

      猜你喜欢
      • 2013-07-01
      • 2013-04-29
      • 2016-04-09
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多