Given a Model, how to loop through all properties?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)