【问题标题】:Best way to return a functions string in another method?以另一种方法返回函数字符串的最佳方法?
【发布时间】:2021-08-19 17:24:04
【问题描述】:

我正在尝试在 ruby​​ 程序中简化/减少一堆重复的 SQL 代码,因此我将重复的代码行放在自己的函数中。

def companies_data
      """
      companies.data->>'creditor_number' as creditor_number,
      companies.data -> 'pay_to_bank_account' ->> 'iban' AS iban,
      company_invoices.from_date,
      company_invoices.to_date,
      concat(company_invoices.r_number, company_invoices.s_number) as rechnungsnummer,
      companies.send_invoice_automatically,
      companies.claim_netting,
      ROUND(company_invoices.total_cents)/100 AS netting_betrag,
      ROUND(company_invoices.billable_dca_fees_recovered_to_creditor_cents + company_invoices.billable_dca_fees_recovered_to_creditor_vat_cents + company_invoices.billable_dca_flat_fees_cents + company_invoices.billable_dca_flat_fees_vat_cents + company_invoices.billable_dca_success_fees_cents + company_invoices.billable_dca_success_fees_vat_cents + company_invoices.billable_tax_free_dca_costs_cents + company_invoices.billable_taxable_dca_costs_cents + company_invoices.billable_taxable_dca_costs_vat_cents)/100 AS rechnungsbetrag,
      round(company_invoices.billable_dca_fees_recovered_to_creditor_cents + company_invoices.billable_dca_fees_recovered_to_creditor_vat_cents + company_invoices.billable_dca_flat_fees_cents + company_invoices.billable_dca_flat_fees_vat_cents + company_invoices.billable_dca_success_fees_cents + company_invoices.billable_dca_success_fees_vat_cents + company_invoices.billable_tax_free_dca_costs_cents + company_invoices.billable_taxable_dca_costs_cents + company_invoices.billable_taxable_dca_costs_vat_cents - company_invoices.total_cents)/100 AS abrechnungsbetrag,
      case_files.currency,
      companies.vat_perspective,
      companies.creditor_payout_by_case_file_invoice
      """

然后我希望能够在其他函数中打印该字符串,例如

def query_for_non_vat_payer

      "SELECT
      distinct NAME,
      #{companies_data}
      FROM case_file_invoices
      JOIN case_files ON case_file_invoices.case_file_id = case_files.id
      JOIN companies ON companies.id = case_files.company_id
      JOIN company_invoices ON company_invoices.id = case_file_invoices.company_invoice_id

      WHERE date_part('year', company_invoices.to_date) = 2021
      AND date_part('month', company_invoices.to_date) = 07
      AND companies.vat_perspective = 'not_vat_payer'
      and companies.invoicing_cycle = 'monthly'"
    end

#{companies_data} 真的是最好的方法吗?

【问题讨论】:

  • 这种方法没有错。分解查询是一个很好的方法。你为什么不使用 ActiveRecord 查询?您仍然可以在 AR 查询中注入 Companies_data。

标签: sql ruby-on-rails ruby


【解决方案1】:

这取决于您所说的“最佳”。在您的情况下,companies_data 不进行任何计算,但始终返回相同的字符串。在这种情况下,我会发现没有函数而是常数更自然:

COMPANIES_DATA = ....

但我不知道你的对象模型。如果您在一个类层次结构中,其中query_for_non_vat_payer 在父类中,并且允许/期望子类提供自己的companies_data 版本,那么方法当然是要走的路。

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 2015-10-09
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 2012-06-26
    相关资源
    最近更新 更多