如何分组关联错误信息?Rails多态关联场景技术问询
解决方案:分组关联错误或提取模型自身错误
针对你的问题,我分两个部分来给出具体解决办法:
一、将Address的错误信息分组为嵌套结构
Rails默认会把关联模型的错误以address.city这种扁平键的形式放到父模型的errors集合里,要转换成你期望的嵌套格式,可以在User模型里自定义一个方法来重新整理错误哈希:
class User < ApplicationRecord has_one :address, as: :addressable accepts_nested_attributes_for :address # 若你使用了嵌套属性提交关联数据 def nested_errors error_hash = errors.to_h address_errors = {} # 提取所有以"address."开头的错误键,整理到address子哈希中 error_hash.each_key do |key| next unless key.start_with?('address.') attribute = key.gsub('address.', '') address_errors[attribute] = error_hash.delete(key) end # 仅当存在address错误时,添加嵌套节点 error_hash[:address] = address_errors unless address_errors.empty? error_hash end end
之后调用user.nested_errors.to_json,就能得到你想要的嵌套格式结果:
{"name":["can't be blank"],"address":{"city":["can't be blank"],"state":["can't be blank"],"country":["can't be blank"],"zipcode":["can't be blank","is invalid"],"business_phone":["is invalid"]}}
二、获取User模型自身的错误(排除Address的错误)
如果只需要User自身的错误,不需要关联模型的错误,有两种简单可行的方式:
方法1:过滤错误哈希中的关联键
直接从errors.to_h中剔除所有以address.开头的键:
user_self_errors = user.errors.to_h.reject { |key, _| key.start_with?('address.') } # 输出结果:{"name":["can't be blank"]}
方法2:利用模型属性列表筛选
只保留属于User自身数据库属性的错误(需替换为你的User实际属性):
user_attributes = User.attribute_names # 这会获取User模型所有数据库字段 user_self_errors = user.errors.to_h.slice(*user_attributes)
另外补充一点:当你调用user.valid?之后,user.address.errors本身就已经包含了Address的所有错误,结合上面的方法提取User自身错误,就能轻松分开处理两类错误了。
内容的提问来源于stack exchange,提问作者gates
相关产品推荐
相关产品推荐

