【发布时间】:2025-12-15 05:10:02
【问题描述】:
一直在jbuilder.json模板中写这样的代码很痛苦:
json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at
所以我想这样编码;
json.extract_all! notification
我发现我可以像下面的代码那样做,但它们对我来说仍然有点冗长。
notification.attributes.each do |key, value|
json.set!(key, value)
end
有没有更好的办法?
【问题讨论】:
-
可能这个简单一点,
json.extract! notification, *notification.attributes.keys但是如果Jbuilder有这样的方法,我想用它来代替。