如何向依赖于其他模型的rails模型添加验证(How to add a validation to rails model which depends on another model)

编程入门 行业动态 更新时间:2024-10-21 12:58:05
如何向依赖于其他模型的rails模型添加验证(How to add a validation to rails model which depends on another model)

无法找到最好的方法来轻松完成这项工作我想为以下问题提出数据库模型。

有一个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 end

I 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 end

May 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

更多推荐

本文发布于:2023-07-24 10:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   依赖于   rails   add   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!