ruby-on-rails - Rails 模型范围,从 3.2.12 更新到 4.2.11 - 范围主体需要可调用
问题描述
我正在将应用程序从 ruby ~3 更新到 ruby ~4(说来话长,但它需要更新,并且由于是来自地狱的遗留应用程序,因此无法超越此范围)。制作这个的开发者早就搬到了更绿色的牧场上,并且不是一个大评论者。
范围包括对另一个模型、记录的引用,并且该记录的范围是任务。抛出范围主体需要可调用的错误
原件:
scope :prod_review, includes(:records=>:task).active.where(
Task.table_name + '.training_area_id = ? AND ' + Record.table_name + '.final_date is not null ',
'msvs 003'
)
这会在包含时引发错误。
scope :prod_review, -> { includes(:records=>:task).active.where(
Task.table_name + '.training_area_id = ? AND ' + Record.table_name + '.final_date is not null ',
'msvs 003'
) }
抛出指向 -> 的语法错误
scope :production_review, includes(:records=>:task).active -> { where(
Task.table_name + '.training_area_id = ? AND ' + QualificationRecord.table_name + '.final_date is not null ',
'msvs 003'
) }
我知道我需要将其更新为 lambda,但无法使其正常工作。
重制为def:
我得到了无效的 SQL 语法。红宝石吐出:
def self.production_review, includes(:qualification_records=>:task).active.where(
Task.table_name + '.training_area_id = ?,
'msvs 003'
)
Generates this SQL:
# scope :ecr_production_review, includes(:qualification_records=>:task).active.where(
# Task.table_name + '.training_area_id = ? AND ' + QualificationRecord.table_name + '. is not null ',
# 'msvs 003'
# )
SELECT [QualityDC].[dbmaster].[employees].* FROM [QualityDC].[dbmaster].[employees] WHERE (NOT status = 4 AND NOT status = 5) AND (QualityDC.dbmaster.Tasks.training_area_id = 'msvs 003') ORDER BY last_name, first_name
QualityDC.dbmaster.Tasks.training_area_id 应该是一个连接并查找值为“msvs 003”的列
解决方案
更新为使用对返回连接结果的相应模型的方法调用