无法找到最好的方法来轻松完成这项工作我想为以下问题提出数据库模型。
有一个Deal表,其中有一个关联的帐户表。 每个帐户可以有多个联系人。 现在,一笔交易需要分配一个主要联系人,该主要联系人必须是相关帐户的多个联系人中的一个。 如何确保主要联系人是其中一个帐户联系人。
Deal Table account_id primary_contact_id Account Table name and other params Contact Table account_id phone, email etc.例如。 我目前使用的课程
class Deal < ActiveRecord::Base belongs_to :account belongs_to :contact end class Account < ActiveRecord::Base has_many :contacts has_many :deals end class Contact < ActiveRecord::Base belongs_to :account has_many :deals end我可以在交易模型或控制器中添加验证,以确保添加的联系人是其帐户联系人之一。 但如何照顾以下情况:
从帐户中删除联系人应确保将交易表的相应contact_id设置为nil 删除与交易相关联的帐户应确保该交易表的contact_id无效 更新帐户关联应确保交易的contact_id无效。Can't find a best way of getting this done easily I want to come up with database model for the following problem.
There is a Deal table which has one associated account table. Each account can have many contacts. Now one deal need to assign a primary contact which must be one among the many contacts of the associated account. How to make sure that the primary contact is one of the account contacts.
Deal Table account_id primary_contact_id Account Table name and other params Contact Table account_id phone, email etc.Eg. class which I use currently
class Deal < ActiveRecord::Base belongs_to :account belongs_to :contact end class Account < ActiveRecord::Base has_many :contacts has_many :deals end class Contact < ActiveRecord::Base belongs_to :account has_many :deals endI can add validation in deal model or controller to make sure that contact getting added is one among its account contacts. But how to take care of following cases:
Deleting a contact from an account should make sure that corresponding contact_id of the deal table is set to nil Deleting an account associated with a deal should make sure that contact_id of that deal table is nullified Updating an account association should make sure that contact_id of the deal is nullified.最满意答案
也许你可以使用模型回调,例如:
class Deal < ActiveRecord::Base belongs_to :account belongs_to :contact before_update :nullify_contact_association, :if => lambda{|i| i.account_id_changed?} private # Nullify contact_id of the deal if it's account association was changed def nullify_contact_association self.contact_id = nil end end class Account < ActiveRecord::Base has_many :contacts has_many :deals before_destroy :nullify_dependencies private #Deleting an account associated with a deal should #make sure that contact_id of that deal table is nullified def nullify_dependencies self.deals.update_all(:contact_id => nil) if deal.present? end end class Contact < ActiveRecord::Base belongs_to :account has_many :deals before_destroy :nullify_dependencies private #Deleting a contact from an account should make sure #that corresponding contact_id of the deal table is set to nil def nullify_dependencies self.deals.update_all(:contact_id => nil) if account.present? end endMay be you could use model callbacks, e.g.:
class Deal < ActiveRecord::Base belongs_to :account belongs_to :contact before_update :nullify_contact_association, :if => lambda{|i| i.account_id_changed?} private # Nullify contact_id of the deal if it's account association was changed def nullify_contact_association self.contact_id = nil end end class Account < ActiveRecord::Base has_many :contacts has_many :deals before_destroy :nullify_dependencies private #Deleting an account associated with a deal should #make sure that contact_id of that deal table is nullified def nullify_dependencies self.deals.update_all(:contact_id => nil) if deal.present? end end class Contact < ActiveRecord::Base belongs_to :account has_many :deals before_destroy :nullify_dependencies private #Deleting a contact from an account should make sure #that corresponding contact_id of the deal table is set to nil def nullify_dependencies self.deals.update_all(:contact_id => nil) if account.present? end end更多推荐
发布评论