# Concatenated fields in a model

**URL:** <https://www.ruby-forum.com/t/concatenated-fields-in-a-model/131298>\
**Category:** Rails\
**Created:** [March 7, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/concatenated-fields-in-a-model/131298 "2008-03-07T20:17:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 7, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/concatenated-fields-in-a-model/131298/1 "2008-03-07T20:17:17Z")

</div>

Hello RoR community from a newbie!

I have a document number “database” in which each document number  
consists of 3 parts: a 2 digit “document category” prefix, a 5 digit  
document number, and a 3 digit “document variation” suffix. (I quoted  
the word “database” because it is currently stored in a single  
spreadsheet). I can export that database to a CSV file and run a  
simple rake rule to load the CSV data into a model. (As a sidebar, if  
anybody wanted to point me in the direction of how to extract the data  
directly from the Excel spreadsheet instead of going through the CSV  
pipe first, I would be grateful.)

Traditionally, our documents have been identified as “01-00032-000” or  
“20-01017-013”, and the data for the document number is stored in 3  
columns in the spreadsheet. I can create my model with three separate  
columns based on the 3 columns from the spreadsheet, or I can  
concatenate them together as shown above. Ultimately, I would like to  
be able to display the list of documents grouped by document category  
(the 2 digit prefix), and perhaps even limit the display to those  
documents that fall into a certain category (00, 01, 02-09, 10-19,  
etc…). (As another aside, would I be better off creating a table  
identifying names of the 10-12 different categories by min/max  
category prefix, or better off coding those names in my specialized  
app?)

I started off prototyping a model with the 3 columns separated and  
then naively combined them in my view with:

\<%=h sprintf("%02d-%05d-%03d", document.document\_category\_prefix,  
document.primary\_document\_number,  
document.document\_variation) %\>

It was quick. It was simple. It tought me that I can embed calls to  
‘sprintf()’ in my view. I went to bed happy.

Today I started investigating active scaffolding so I could get a much  
prettier view of the documents than the bare view shown by the default  
scaffolding. (I reasoned that, if the interface looks ugly, folks  
will think that the implementation is kludgy and buggy, but if it  
looks beautiful, they’ll think I’m a genios.) (That’s a joke, son.)  
I quickly ran into the issue of trying to figure out how I would  
combine the 3 columns of data in a nice sprintf() output with Active  
Scaffolding. The answer to that problem may be trivial, or it may  
not, but it quickly led me to my real question:

Should I be combining data like this in a view in the first place? Or  
should that go somewhere in the model or controller? If so, how would  
I do that? (Asked in the tone of somebody who wrote his first RoR  
application last night.)

I would appreciate any wisdom folks would care to share in this matter.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 7, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/concatenated-fields-in-a-model/131298/2 "2008-03-07T21:09:51Z")

</div>

On 7 Mar 2008, at 19:16, Patrick D. wrote:

> Should I be combining data like this in a view in the first place? Or  
> should that go somewhere in the model or controller? If so, how would  
> I do that? (Asked in the tone of somebody who wrote his first RoR  
> application last night.)

Have you looked at composed\_of ?

Fred

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [March 7, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/concatenated-fields-in-a-model/131298/3 "2008-03-07T21:41:38Z")

</div>

On Mar 7, 2:16 pm, “Patrick D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (As a sidebar, if  
> anybody wanted to point me in the direction of how to extract the data  
> directly from the Excel spreadsheet instead of going through the CSV  
> pipe first, I would be grateful.)

Have you looked at parseexcel?

[http://www.rubyrescue.com/?p=6](http://www.rubyrescue.com/?p=6)

Other options: If you’re on the Windows Platform, there’s win32ole  
which can automate excel. If you’re on JRuby, you can interface with  
POI for excel parsing and creation.

- Mark.
