Loop over Object attributes in Ruby on Rails

The ActiveRecord::Base.attributes() method returns a hash of all the attributes. You can use that to loop over all attributes.

@work_profile.attributes.each do |attr_name, attr_value|
  ...
end

In a view, this would give:

<% @work_profile.attributes.each do |attr_name, attr_value| %>
  <div class="work_profile_attribute">
    <%= attr_name %>: <%= attr_value %>
  </div>
<% end %>

Leave a Comment

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