ruby-on-rails – 验证has_and_belongs_to_many的存在
发布时间:2020-12-17 03:33:49 所属栏目:百科 来源:网络整理
导读:嗨,我在模型中使用has_and_belongs_to_many. 我想设置各种存在的价值. 并将每个核心的最大种类数设置为3 class Core ActiveRecord::Base has_and_belongs_to_many :kinds,:foreign_key = 'core_id',:association_foreign_key = 'kind_id'end 我能怎么做? 谢
嗨,我在模型中使用has_and_belongs_to_many.
我想设置各种存在的价值. 并将每个核心的最大种类数设置为3 class Core < ActiveRecord::Base has_and_belongs_to_many :kinds,:foreign_key => 'core_id',:association_foreign_key => 'kind_id' end 我能怎么做? 谢谢 解决方法validate :require_at_least_one_kind validate :limit_to_three_kinds private def require_at_least_one_kind if kinds.count == 0 errors.add_to_base "Please select at least one kind" end end def limit_to_three_kinds if kinds.count > 3 errors.add_to_base "No more than 3 kinds,please" end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |