我有一个活动记录,我想在一个单独的数组中收集它的密钥
所以,如果我的@item.first
有
item.a = 1
item.b = 2
item.c = 3
我想收集像[a => 1, b => 2, c => 3]
这样的数组。
有办法做到这一点吗?
答案 0 :(得分:2)
可以使用attributes
:
@item.first.attributes
要选择特定属性,您可以使用select
过滤为:
@item.first.attributes.select { |key| ['a', 'b', 'c'].include?(key) }
答案 1 :(得分:0)