使用 SQL 將方法邏輯移動到 Rails 項目中的模型 (Moving method logic with SQL to model in rails project)


問題描述

使用 SQL 將方法邏輯移動到 Rails 項目中的模型 (Moving method logic with SQL to model in rails project)

將我的控制器 (Invoices) 中的方法移動到相應的模型中,但我遺漏了一些東西。我試過關注 thisthis 甚至 this 還有一些小問題需要我多加註意。

我的工作控制器方法是這個。

  def array_of_disbursable_invoices
    sql = "SELECT MIN(departure_date), ch_invoice.invoice_id
    FROM ch_invoice
    INNER JOIN ch_trip
    ON ch_invoice.invoice_id = ch_trip.invoice_id
    WHERE departure_date <= (SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP)FROM DUAL)
    AND service_rendered = 0
    AND paid = 1
    Group By ch_invoice.invoice_id"

    report = ActiveRecord::Base.connection.exec_query(sql)
    render json: report
  end

我正試圖把它變成這個。

  def array_of_disbursable_invoices
    report = report.array_of_disbursable_invoices
    render json: report
  end

這裡有我模型中的邏輯。<


參考解法

方法 1:

You beat me to it (and went, essentially, with my second option). But, I'll post this anyway.

When you do this:

def array_of_disbursable_invoices
  report = report.array_of_disbursable_invoices
  render json: report
end

You're calling array_of_disbursable_invoices on an instance. But, you don't instantiate Report ‑ thus, the undefined method 'array_of_disbursable_invoices' for nil:NilClass error.

So, I think you have two choices:

(1) You could call the method on an instance, something like:

report = Invoice.new.array_of_disbursable_invoices

(2) You could make the method a class method, something like:

class Invoice < ActiveModel::Base
  class << self
    def array_of_disbursable_invoices
      sql = "SELECT MIN(departure_date), ch_invoice.invoice_id
      FROM ch_invoice
      INNER JOIN ch_trip
      ON ch_invoice.invoice_id = ch_trip.invoice_id
      WHERE departure_date <= (SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP)FROM DUAL)
      AND service_rendered = 0
      AND paid = 1
      Group By ch_invoice.invoice_id"

      connection.exec_query(sql)
    end
  end
end

I think I'd recommend (1). Also, personally, I'd use the ActiveRecord query interface instead of the SQL (assuming you have all the associations set up in your models). But, that's a personal preference.

方法 2:

Got it to work with the following code in my controller.

  def array_of_disbursable_invoices
    report = Invoice.array_of_disbursable_invoices
    render json: report
  end

And this in the model.

  def self.array_of_disbursable_invoices
    sql = "SELECT MIN(departure_date), ch_invoice.invoice_id
    FROM ch_invoice
    INNER JOIN ch_trip
    ON ch_invoice.invoice_id = ch_trip.invoice_id
    WHERE departure_date <= (SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP)FROM DUAL)
    AND service_rendered = 0
    AND paid = 1
    Group By ch_invoice.invoice_id"

    ActiveRecord::Base.connection.exec_query(sql)
  end

(by CheeseFryjvillianCheeseFry)

參考文件

  1. Moving method logic with SQL to model in rails project (CC BY‑SA 2.5/3.0/4.0)

#controller #Model #ruby-on-rails #ruby #refactoring






相關問題

覆蓋我的控制器,我應該在哪個事件中檢查 cookie? (Overriding my controller, which event should I check for a cookie in?)

ASP.NET MVC,將模型從視圖傳遞到控制器 (ASP.NET MVC, passing Model from View to Controller)

Ruby on Rails:更改列表順序後未顯示錯誤消息 (Ruby on Rails: Error messages not displaying since changing order of a list)

Успадкоўванне кантролера ASP.NET MVC 4 (ASP.NET MVC 4 controller inheritance)

如何在 CakePHP 中創建自定義 MySQL 查詢? (How to create custom MySQL queries in CakePHP?)

Respond_to 沒有調用正確的格式 (Respond_to does not call the right format)

Rails - 尋找錯誤 ID 的 JSON 回調 (Rails - JSON callback looking for wrong id)

YIi PHP - 帶有 foreach 循環的輸出數組 (YIi PHP - Output array with a foreach loop)

使用 SQL 將方法邏輯移動到 Rails 項目中的模型 (Moving method logic with SQL to model in rails project)

MVC 為什麼要單元測試控制器 (MVC Why unit test controllers)

Jquery & Rails 3:從 js 文件運行控制器操作? (Jquery & Rails 3: Run controller action from js file?)

.net core mvc:在區域上添加具有 crud 功能的控制器未正確生成代碼和鏈接 (.net core mvc: adding controller with crud functionality on area didn't generate code and link correctly)







留言討論