Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:06:50+00:00 2026-06-10T11:06:50+00:00

I have items (documents) in Ruby on Rails that have zero or many attributes.

  • 0

I have items (documents) in Ruby on Rails that have zero or many attributes. The document attribute has a type (“Author”, “Publisher” etc). Each document can have multiple attributes of the same type.

These are currently just pulled out the the db and listed as follows:

<% @document.attribute_documents.each do |doc_attr| %>
  <b><%= doc_attr.attribute_type.try(:name) %>: </b>
  <%= "  #{doc_attr.value}" %>
  <%= ", #{doc_attr.attribute_location}" unless doc_attr.attribute_location.blank? %>
  <%= ", #{doc_attr.attribute_year}" unless doc_attr.attribute_year.blank? %><br />
<% end %>

Which gives a result like this, less than satifactory:

Publisher: William Cavell, Holborn, London
Printer: William Cavell, Holborn, London
Seller: J Barber, Newcastle
Seller: C Etherington, York
Seller: Fletcher and Hodson, Cambridge
Seller: Wilson, Dublin

What I am looking for is this:

Publisher: William Cavell, Holborn, London
Printer: William Cavell, Holborn, London
Sellers: J Barber, Newcastle, C Etherington, York, Fletcher and Hodson, Cambridge, Wilson, Dublin

So I need to group on attribute type, and then loop through the values depending on the type. Is there a simple way to doing this in RoR? Is my approach all wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:06:52+00:00Added an answer on June 10, 2026 at 11:06 am

    Use rails’ group_by method:

    <% @document.attribute_documents.group_by { |d| d.attribute_type.try(:name) }.each_pair do |type, doc_attrs|
      <b><%= type %>: </b>
      <% doc_attrs.each do |doc_attr| %>
        <%= "  #{doc_attr.value}" %>
        <%= ", #{doc_attr.attribute_location}" unless doc_attr.attribute_location.blank? %>
        <%= ", #{doc_attr.attribute_year}" unless doc_attr.attribute_year.blank? %><br />
      <% end %>
    <% end %>
    

    Haven’t tested this, but I think it should work.

    UPDATE: This will leave out commas between entries in each line. I’ll come back to it later, basically you need to use map and then join on the doc_attrs array.

    UPDATE(2):

    Here’s the expression that I was looking for, to go in the doc_attrs.each loop:

    doc_attrs.map do |doc_attr|
      [:value, :attribute_location, :attribute_year].map do |attr|
        doc_attr.send(attr)
      end.delete_if(&:blank?).join(", ")
    end.join(", ")
    

    I don’t use erb anymore so not quite sure how to translate it, but probably better anyway to create a helper function and build it up that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in .gsp file i have javaScript function <script type=text/javascript> function getCurrentItemNumber(){ return document.getElementById('item_itemNumber').innerHTML.substr(6); }
I have items in a list in a SharePoint 2003 site that I need
I have an element that have items and a tag. The element can contain
If i have items stored in a database that users can download after they
WPF controls have certain properties (UserControl.Resources, UserControl.CommandBindings) that can have items added to them
I have some items in my .emacs that I don't want to run if
I have a Rails 3 model D that belongs to another model, which belongs
I have list items that displayed inline. I want to align them vertically inside
There are laws that have established forms of legal documents. An idea is to
I am modifying a Json document through code. The Json has array nested items.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.