I want to loop through all the properties of my 'user' model, how can I do this?
If you have an instance of your model then user.attributes
is a Hash of the model's attributes and their values so, for example, you can do something like:
user.attributes.each_pair do |name, value| puts "#{name} = #{value}" end
If you don't have a specific instance then the class has methods that return information about the fields in the database e.g. User.columns
and User.content_columns
. e.g.
User.columns.each do |column| puts column.name end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With