Iterate over ActiveRecord object properties

I am trying to iterate over the properties of an ActiveRecord object. I searched on the Internet, but I’m coming up all the time. I thought I was warm with object.to_enum, but despite the fact that this returns an Enumerator object, I still get the error message "every" undefined for my object.

Calling object.methods gives me a bunch of inherited things that I don't want. Calling object.instance_variables does not give me anything close to what I'm looking for. I guess this is not difficult, but I have not figured it out yet. Can anyone help? Thank!

+3
source share
2 answers

Have you tried object.attributes? This will give you a hash of all the attributes of the object matching the attribute name with the attribute value.

+9

Source: https://habr.com/ru/post/1771522/


All Articles