has_many仅软删除记录的关联



我想为具有一些deleted_at not 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:nil 而不是 deleted_at != nil 的记录

有什么方法只能从 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,它将只返回 ID 为 3 的贷款申请记录。

更改贷款应用程序模型:

has_many :applicant_co_applicants, -> { not_deleted }

将此添加到申请人共同申请人模型:

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

相关内容

  • 没有找到相关文章