0
我在我的Job
模型中創建了一些作用域,ActiveAdmin將使用這些作用域篩選管道各個階段的作業。具有多個連接和has_one關聯的Rails ActiveRecord作用域
我不得不創建連接到其他表來收集相關信息,這就是我遇到麻煩的地方。我在下面概述了我的邏輯和(非工作)嘗試。
我將如何重構這些以使它們正常工作?
# app/models/job.rb
# where job.survey exists AND job.appointment.appointment_date is in past AND job.quote doesn’t exist
scope :awaiting_quote, -> { joins(:survey).
joins('LEFT OUTER JOIN quotes ON quote.job_id = jobs.id').
joins('LEFT OUTER JOIN appointments ON appointment.job_id = jobs.id').
where('appointment.appointment_date < :current_time', { current_time: Time.current }).
where('survey.id IS NOT NULL').
where('quote.id IS NULL')
}
# where job.quote exists AND job.quote_accepted is false
scope :awaiting_acceptance, -> { joins(:quote).
joins('LEFT OUTER JOIN appointments ON quote.job_id = jobs.id').
where('appointment.appointment_date < :current_time', { current_time: Time.current }).
where('quote.id IS NOT NULL').
where('quote_accepted = :quote_accepted', { quote_accepted: false })
}
has_one :quote
has_one :survey
has_one :appointment
has_one :surveyor, through: :appointment
accepts_nested_attributes_for :appointment, :allow_destroy => true
accepts_nested_attributes_for :survey, :allow_destroy => true
# app/models/quote.rb
belongs_to :job
# app/models/survey.rb
belongs_to :job
# app/models/appointment.rb
belongs_to :job
belongs_to :surveyor
# app/models/surveyor.rb
has_many :appointments
has_many :jobs, through: :appointments
你可以發佈你的型號代碼嗎?鑑於良好的模型/關聯設計,這似乎不太複雜。 – errata
當然,我已經用我的模型代碼更新了這個問題。 – gosseti
你有沒有想過使用類方法呢? – fatfrog