Reputation: 3489
I've got an active record to which I want to collect its keys in a seperate array
So if my @item.first
has
item.a = 1
item.b = 2
item.c = 3
I want to collect an array like [a => 1, b => 2, c => 3]
.
Is there a way to do this?
Upvotes: 2
Views: 4741
Reputation: 2328
yes you can do it using as_json read this http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json
Upvotes: 0
Reputation: 38645
This can be done with attributes
:
@item.first.attributes
And to select specific attributes you can filter with select
as:
@item.first.attributes.select { |key| ['a', 'b', 'c'].include?(key) }
Upvotes: 4