【问题标题】:How to initialize the own class in rails?如何在rails中初始化自己的类?
【发布时间】:2009-06-03 11:12:33
【问题描述】:

我想调用 InvoicingAndDelivery.report_lines,然后给我这些结果的数组,但它不起作用。有任何想法吗?

class InvoicingAndDelivery < Report
  include DateExtentions
  attr_accessor :report_lines, :tba_line, :sum_line

  def initialize(part_or_service_ids, start_date, to_date)

    # @report_lines = []

    number_of_months(start_date,to_date).times do |i|
      start_date = start_date + i.months
      l = InvoicingAndDeliveryReport.new
      l.month = start_date.strftime("%m/%Y")
      l.work_profile = WorkProfileLine.delivery_amount_for_serivces_in(part_or_service_ids, start_date)
      l.forecast_delivery = LineItem.forecast_delivery_amount_for_services_in(part_or_service_ids, start_date)
      l.invoicing = InvoiceLine.invoices_total_for_services_in(part_or_service_ids, start_date)
      l.projected_invoices = InvoiceLine.projected_invoice_total_for_services_in(part_or_service_ids, start_date)
      l.costs =  LineItem.costs_total_for_services_in(part_or_service_ids, start_date)
      l.gross_profit = l.invoicing - l.costs
      l.opportunities = LineItem.oportunities_value_for_services_in(part_or_service_ids, start_date)
      #    @report_lines<< l
    end

    @tba_line = InvoiceLine.service_type(@part_or_service).tba_lines.sum("invoice_lines.sell_price * invoice_lines.quantity").to_f
    @sum_line = get_sum_line
  end

【问题讨论】:

    标签: ruby-on-rails ruby oop


    【解决方案1】:

    什么是报告?

    您有可能重写了您不应该有的东西...需要在初始化时调用 super,或者如前所述,将其设为回调。

    【讨论】:

    • 我真的需要阅读一些关于如何使用抽象类以及如何使用超级的书。谢谢你的回答。
    【解决方案2】:

    如果InvoicingAndDeliveryActiveRecord::Base 的后代,那么你就是shouldn't override initialize。而是将此代码放入after_initialize

    【讨论】:

    • 谢谢,我没有将这个类用作 ActiveRecord,但无论如何感谢您的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多