【问题标题】:How to traverse associations如何遍历关联
【发布时间】:2019-12-05 12:13:51
【问题描述】:

我发现在 Active Admin 中遍历关联很棘手。

在我的应用程序中,我有一个出现在发票上的 SupportSession。通过与 SupportAllocation 的关联,我可以遍历链以获取在 SupportSession 上可收取的 SupportRate(s)。我可以在计算发票价值时使用这些费率。

我的模型是:

class Invoice < ApplicationRecord
  has_one :support_session

class SupportSession < ApplicationRecord
  belongs_to :invoice, optional: true
  belongs_to :support_allocation

class SupportAllocation < ApplicationRecord
  has_many :support_sessions
  has_many :support_rates

class SupportRate < ApplicationRecord
  belongs_to :support_allocation

我为 Invoice 创建了一个 Active Admin 资源,我想在其中进行一些计算:

ActiveAdmin.register Invoice do

  index do
    selectable_column

    column 'Reference', :reference, :sortable => 'invoices.reference'
    column 'Raised', :created_at, :sortable => 'invoices.created_at' do |invoice|
      invoice.created_at.strftime('%Y-%m-%d')
    end
    column 'Due by', :due_by, :sortable => 'invoices.due_by'

    column :value do |invoice|
      # The next line retrieves an array of SupportRate(s) associated with this SupportSession on this invoice
      invoice.support_session.support_allocation.support_rates.each do |support_rate|

      # If the current SupportSession's support_type matches the SupportRate's support_type
        if support_rate.support_type == invoice.support_session.support_type
          # Do some calculations based on the support_rate.price attribute, plus some other bits I've omitted
        end
      end
      # The next line returns the correct price but obviously it's too clumsy and doesn't do the match on support_type
      # invoice.support_session.support_allocation.support_rates[0].price
    end

    actions
  end

end 

我可以看到正在正确检索数据。我也可以看到它是一个数组。但是如果我尝试用它做任何事情,例如在我的“如果”条件下打印出 support_rate.price,我只是得到(例如第一条记录):

[#<SupportRate id: 3, support_type: "mentoring", price: 0.3e2, support_allocation_id: 2>, #<SupportRate id: 13, support_type: "study_skills", price: 0.45e2, support_allocation_id: 2>]

在这个特定示例中,匹配的 support_type 是“study_skills” - 我需要使用该 support_rate 的价格进行计算。

我想解决方案在于通过数组或匹配率进行某种循环,就像我尝试过的那样? Active Admin 似乎不喜欢它。

感谢您的帮助。

【问题讨论】:

  • 请详细说明Active Admin doesn't seem to like it though.。错误或意外结果是什么?
  • 意外结果出现在代码块中,在原始帖子中,在“我刚刚得到(例如第一条记录):”之后不久

标签: ruby-on-rails ruby ruby-on-rails-5 activeadmin


【解决方案1】:

我通过调用可用关联的查询(find_by 方法)解决了这个问题:

Invoice.calculate_value(invoice.support_session.support_allocation.support_rates.find_by(support_type: invoice.support_session.support_type).price

我记得关联是一个对象,因此它继承了所有可用的常用查询方法。

为了进行必要的计算,我刚刚创建了一个自定义函数,我将值传递给它,然后它返回一个计算值:

column :value do |invoice|
      Invoice.calculate_value(invoice.support_session.support_allocation.support_rates.find_by(support_type: invoice.support_session.support_type).price, invoice.support_session.rounded_duration_mins)
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多