gpt4 book ai didi

ruby-on-rails - 验证模型子类中的关联

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:02 24 4
gpt4 key购买 nike

我在验证派生类中关联的存在时遇到了一个奇怪的问题。

Class Contact < ActiveRecord::Base

has_and_belongs_to_many :things

end

Class SubContact < Contact

# validates_presence_of :things
validate :must_have_things

def must_have_things
if things.blank?
errors.add("Must select things")
end
end

end

SubContact 类中,我试图验证事物的存在,但没有任何效果。我已经尝试过自定义和内置验证器我怎样才能做到这一点?

最佳答案

add 需要一个属性。尝试使用:

errors.add(:base, "Must not be blank")

在这种情况下,它是基类,但它也可以是任何其他属性。提及 SubContract 类或 :base 的属性名称。

关于ruby-on-rails - 验证模型子类中的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com