gpt4 book ai didi

ruby - 验证关联模型的特定属性

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

验证关联模型属性的最简单的 Railsy 方法是什么?

Item
belongs_to :user
validates_presence_of :user
# AND the "is_photographer" column for that user must be true

User
has_many :items
# can be a regular user or a photographer

最佳答案

validate :user_is_photographer, :if => :user

def user_is_photographer
errors.add(:user, "should be a photographer") unless user.is_photographer
end

关于ruby - 验证关联模型的特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426744/

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