将 SQL 的方法逻辑移动到 rails 项目中的模型
Moving method logic with SQL to model in rails project
将我的控制器 (Invoices
) 中的一个方法移动到相应的模型中,但我遗漏了一些东西。我已经尝试关注 this and and even 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
在这里使用我模型中的逻辑。
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"
ActiveRecord::Base.connection.exec_query(sql)
end
当前错误信息
undefined method `array_of_disbursable_invoices' for nil:NilClass
让它在我的控制器中使用以下代码。
def array_of_disbursable_invoices
report = Invoice.array_of_disbursable_invoices
render json: report
end
模型中也是如此。
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
你先于我(并且基本上选择了我的第二个选项)。但是,无论如何我都会post这个。
当你这样做时:
def array_of_disbursable_invoices
report = report.array_of_disbursable_invoices
render json: report
end
您正在对实例调用 array_of_disbursable_invoices
。但是,您没有实例化 Report
- 因此, undefined method 'array_of_disbursable_invoices' for nil:NilClass
错误。
所以,我认为你有两个选择:
(1) 您可以在实例上调用该方法,例如:
report = Invoice.new.array_of_disbursable_invoices
(2) 您可以将方法设为 class 方法,例如:
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
我想我会推荐 (1)。另外,就我个人而言,我会使用 ActiveRecord 查询接口而不是 SQL(假设您已经在模型中设置了所有关联)。但是,这是个人喜好。
将我的控制器 (Invoices
) 中的一个方法移动到相应的模型中,但我遗漏了一些东西。我已经尝试关注 this and
我的工作控制器方法是这样的。
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
在这里使用我模型中的逻辑。
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"
ActiveRecord::Base.connection.exec_query(sql)
end
当前错误信息
undefined method `array_of_disbursable_invoices' for nil:NilClass
让它在我的控制器中使用以下代码。
def array_of_disbursable_invoices
report = Invoice.array_of_disbursable_invoices
render json: report
end
模型中也是如此。
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
你先于我(并且基本上选择了我的第二个选项)。但是,无论如何我都会post这个。
当你这样做时:
def array_of_disbursable_invoices
report = report.array_of_disbursable_invoices
render json: report
end
您正在对实例调用 array_of_disbursable_invoices
。但是,您没有实例化 Report
- 因此, undefined method 'array_of_disbursable_invoices' for nil:NilClass
错误。
所以,我认为你有两个选择:
(1) 您可以在实例上调用该方法,例如:
report = Invoice.new.array_of_disbursable_invoices
(2) 您可以将方法设为 class 方法,例如:
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
我想我会推荐 (1)。另外,就我个人而言,我会使用 ActiveRecord 查询接口而不是 SQL(假设您已经在模型中设置了所有关联)。但是,这是个人喜好。