Rails_admin有很多通过删除两者:通过模态

时间:2015-04-23 09:54:28

标签: ruby-on-rails ruby-on-rails-4 has-many-through rails-admin

我有<div class="modal fade " id="testModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">&times;</span> <span class="sr-only">Close</span> </button> <h4 class="modal-title">Text</h4> </div> <div class="modal-body"> <textarea class="form-control ng-pristine ng-valid ng-touched" rows="9" style="margin-top: 15px;" data-ng-model="test.text"></textarea> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-blue" data-dismiss="modal" data-ng-click="save()">Save changes</button> </div> </div> </div> @Entity @Table(name="cart", catalog="db_sports" ) // Define named queries here @NamedQueries ( { @NamedQuery ( name="Cart.countAll", query="SELECT COUNT(x) FROM Cart x" ) } ) @Component @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="session") public class Cart implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id", nullable=false) private Integer id ; private Date date ; //---------------------------------------------------------------------- // ENTITY LINKS ( RELATIONSHIP ) //---------------------------------------------------------------------- @ManyToOne @JoinColumn(name="tenant_id", referencedColumnName="id") private Tenant tenant ; @OneToMany(mappedBy="cart", targetEntity=CartItem.class) private List<CartItem> listOfCartItem; @ManyToOne @JoinColumn(name="user_phoneno", referencedColumnName="phoneno") private User user ; @OneToMany(mappedBy="cart", targetEntity=Invoice.class) private List<Invoice> listOfInvoice; //---------------------------------------------------------------------- // CONSTRUCTOR(S) //---------------------------------------------------------------------- public Cart() { super(); } //to string method //setters and getters } campaigncampaign_commissions模态。

applicant applicant_commissions可用campaign_commissions commissions campaign applicant_commissions 实例 applicants }。

当我删除申请人时,rails_admin还想删除campaign_commissions和applicant_commissions。 只希望commissions被销毁。

如何指定这种情况发生?

我的模特: applicant.rb

applicant_commissions

applicant_commission.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" }

campaign_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.rb(未参与此问题,但已添加到上下文中)

class CampaignCommission < ActiveRecord::Base

  belongs_to :campaign
  belongs_to :commission

  has_many :applicant_commissions
  has_many :applicants, through: :applicant_commissions

end

1 个答案:

答案 0 :(得分:1)

applicant_commissions通过这种关系进入竞选委员会。

has_many :campaign_commissions, through: :applicant_commissions

我建议用这个方法来解决你提出的问题。

has_many :campaign_commissions

并且,我没有在您的示例模型中看到这一点,但为了答案的完整性,我将在此处包含它。如果不包含destroy属性,则不会删除依赖记录(或依赖于关联)。

 has_many :applicant_commissions, :dependent => :destroy

我不认为你可以通过两种方式拥有很多关系,就像在应用程序运行期间一样,但是当你想要删除父记录时暂停关系。但也许有人会得到答案和方法来做到这一点。