Rails_admin 有很多 through 正在删除两个 :through 模态
Rails_admin has many through is deleting both :through modals
我有 campaign
、campaign_commissions
、applicant
和 applicant_commissions
模态。
campaign_commissions
都是 commissions
可用于 campaign
和 applicant_commissions
是 applicants
实例 那些 commissions
。
当我删除一个应聘者时,rails_admin也想同时删除campaign_commissions和applicant_commissions。我只希望 applicant_commissions
被销毁。
如何指定发生这种情况?
我的模特:
applicant.rb
class Applicant < ActiveRecord::Base
enum hired: [:pending, :declined, :awaiting_post, :accepted]
attr_accessor :post_url
belongs_to :post
belongs_to :campaign
belongs_to :site
has_many :applicant_commissions
accepts_nested_attributes_for :applicant_commissions
has_many :campaign_commissions, through: :applicant_commissions
scope :accepted, -> { where('hired == ?', 3) }
scope :declined, -> { where('hired < ?', 1) }
scope :pending, -> { where('hired < ?', 0) }
scope :awaiting_post, -> { where('hired < ?', 2) }
validates :campaign_id, :presence => true
#validates :site_id, :presence => true
validates :applicant_commissions, :presence => true
validates :campaign_id, uniqueness: { scope: :site_id,
message: "We already have an application from you" }
applicant_commission.rb
class ApplicantCommission < ActiveRecord::Base
include ActionView::Helpers::NumberHelper
belongs_to :applicant
belongs_to :campaign_commission
has_many :invoice_parts
#delegate :user, to: applicant
end
campaign_commission.rb
class CampaignCommission < ActiveRecord::Base
belongs_to :campaign
belongs_to :commission
has_many :applicant_commissions
has_many :applicants, through: :applicant_commissions
end
campaign.rb(未涉及问题,为上下文添加)
class Campaign < ActiveRecord::Base
has_attached_file :image, :styles => { :default => "500x500>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
belongs_to :programme
belongs_to :user
has_many :applicants
has_many :campaign_commissions
has_many :commissions, through: :campaign_commissions
has_many :images, as: :imageable
scope :front, -> { limit(4) }
end
applicant_commissions 通过这种关系获得活动佣金。
has_many :campaign_commissions, through: :applicant_commissions
我建议用这个来解决你提出的问题。
has_many :campaign_commissions
而且,我没有在您的示例模型中看到它,但为了答案的完整性,我将其包含在此处。如果不包含 destroy 属性,依赖记录(或关联依赖)将不会被删除。
has_many :applicant_commissions, :dependent => :destroy
我不认为你可以通过两种方式拥有 has many through 关系,就像在申请期间一样 运行 但是当你想删除父记录时暂停关系。但也许有人会给出答案和方法。
我有 campaign
、campaign_commissions
、applicant
和 applicant_commissions
模态。
campaign_commissions
都是 commissions
可用于 campaign
和 applicant_commissions
是 applicants
实例 那些 commissions
。
当我删除一个应聘者时,rails_admin也想同时删除campaign_commissions和applicant_commissions。我只希望 applicant_commissions
被销毁。
如何指定发生这种情况?
我的模特: applicant.rb
class Applicant < ActiveRecord::Base
enum hired: [:pending, :declined, :awaiting_post, :accepted]
attr_accessor :post_url
belongs_to :post
belongs_to :campaign
belongs_to :site
has_many :applicant_commissions
accepts_nested_attributes_for :applicant_commissions
has_many :campaign_commissions, through: :applicant_commissions
scope :accepted, -> { where('hired == ?', 3) }
scope :declined, -> { where('hired < ?', 1) }
scope :pending, -> { where('hired < ?', 0) }
scope :awaiting_post, -> { where('hired < ?', 2) }
validates :campaign_id, :presence => true
#validates :site_id, :presence => true
validates :applicant_commissions, :presence => true
validates :campaign_id, uniqueness: { scope: :site_id,
message: "We already have an application from you" }
applicant_commission.rb
class ApplicantCommission < ActiveRecord::Base
include ActionView::Helpers::NumberHelper
belongs_to :applicant
belongs_to :campaign_commission
has_many :invoice_parts
#delegate :user, to: applicant
end
campaign_commission.rb
class CampaignCommission < ActiveRecord::Base
belongs_to :campaign
belongs_to :commission
has_many :applicant_commissions
has_many :applicants, through: :applicant_commissions
end
campaign.rb(未涉及问题,为上下文添加)
class Campaign < ActiveRecord::Base
has_attached_file :image, :styles => { :default => "500x500>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
belongs_to :programme
belongs_to :user
has_many :applicants
has_many :campaign_commissions
has_many :commissions, through: :campaign_commissions
has_many :images, as: :imageable
scope :front, -> { limit(4) }
end
applicant_commissions 通过这种关系获得活动佣金。
has_many :campaign_commissions, through: :applicant_commissions
我建议用这个来解决你提出的问题。
has_many :campaign_commissions
而且,我没有在您的示例模型中看到它,但为了答案的完整性,我将其包含在此处。如果不包含 destroy 属性,依赖记录(或关联依赖)将不会被删除。
has_many :applicant_commissions, :dependent => :destroy
我不认为你可以通过两种方式拥有 has many through 关系,就像在申请期间一样 运行 但是当你想删除父记录时暂停关系。但也许有人会给出答案和方法。