2017-07-24 85 views
0

我想爲具有deleted_at的某些記錄的模型執行has_many關聯而不是nil,並且我希望只能通過has_many關聯檢索那些記錄,但目前它不工作,我不知道如何解決它。has_many僅適用於軟刪除記錄

class LoanApplication < ActiveRecord::Base 
    acts_as_paranoid 
    has_many :applicant_co_applicants, :class_name => "ApplicantCoApplicant", :foreign_key => "loan_application_id" 
    has_many :co_applicant_infos, :through => :applicant_co_applicants, :source => :loan_application_co_applicant, :dependent => :destroy 
    has_many :removed_co_applicant_infos, :through => :applicant_co_applicants, :source => :loan_application_co_applicant, :dependent => :destroy 
end 

class ApplicantCoApplicant < ActiveRecord::Base 
    acts_as_paranoid 
    attr_accessible :loan_application_id, :co_applicant_id, :deleted_at 

    belongs_to :loan_application_co_applicant, class_name: 'LoanApplication', foreign_key: "co_applicant_id" 
    belongs_to :loan_application, class_name: 'LoanApplication', foreign_key: "loan_application_id" 
end 

我試圖在ApplicationCoApplicant表只檢索的soft_deleted記錄loan_application對象,但我查詢還是搜索僅適用於deleted_at記錄:無,而不是deleted_at =零

有什麼辦法,我可以只能從ApplicantCoAppliant模型獲取soft_deleted記錄並使用它來過濾co_applicant_infos關聯?

感謝感激

**LoanApplication Table** 
id  name deleted_at 
1 person a  nil 
2 co person b  nil 
3 co person c  nil 

**ApplicantCoApplicant Table** 
    id  loan_application_id  co_applicant_id deleted_at 
    1   1      2    nil 
    2   1      3    2017-07-24 02:34:37 

這是我的樣本表。我想一個的has_many關聯,由此當我打電話 LoanApplication.find(1).removed_co_applicant_infos,它將返回LoanApplication只記錄ID爲3

+0

你可以顯示你曾試過的查詢嗎? – Pavan

回答

0

變化LoanApplication型號:

has_many :applicant_co_applicants, -> { not_deleted } 

其添加到ApplicantCoApplicant型號:

scope :not_deleted, -> { where.not('deleted_at' => nil) } 
+0

這不能很好地解決我的問題,但感謝您的幫助。 –

+0

@KingsleySimon我做了一些小改動。你現在得到了什麼結果? – Sajin

+0

仍然無法正常工作。 –

相關問題