# Join fields for list views

**URL:** <https://www.ruby-forum.com/t/join-fields-for-list-views/96975>\
**Category:** Rails\
**Created:** [January 25, 2006, 4:49am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975 "2006-01-25T04:49:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 4:49am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/1 "2006-01-25T04:49:54Z")

</div>

I am sitting with the Agile book on my desk and scratching my head at  
the discussion on aggregation - perhaps that isn’t what I need.

I have a db called clients.  
fields include - first\_name middle\_initial last\_name

I want to combine them all into one name element in a list view. I can  
add/edit the fields separately but in the list view, I only want the one  
combo field.

Is there a documentation page that describes this? I have browsed the  
[rubyonrails.org](http://rubyonrails.org) wiki and haven’t stumbled into an appropriate page.

Pointers to references would be greatly appreciated.

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 6:11am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/2 "2006-01-25T06:11:33Z")

</div>

Craig:

There’s a neat way you can do that in the model. I saw it once but  
was busy and didn’t commit it to memory. That was the semi-bad news.  
The good news is that you can do that in MySQL really easily (I hope  
you are using MySQL

select concat(first\_name," “,middle\_initial,” ",last\_name) as name,  
other fields go here from table where etc.

bruce

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 6:20am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/3 "2006-01-25T06:20:39Z")

</div>

Yeah - I’m looking for the model methodology and I’m using postgres at  
the moment here and I’m thinking that I want to keep it as abstract as  
possible.

thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 6:58am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/4 "2006-01-25T06:58:09Z")

</div>

The Agile book isn’t clear to me.

I have clients database…fields:  
first\_name  
middle\_initial  
last\_name

In client.rb I have

class Client \< ActiveRecord::Base  
has\_one :case\_manager  
has\_many :placements

composed\_of :name,  
:class\_name =\> Name,  
:mapping =\>  
[[first\_name, :first\_name],  
[middle\_initial, :middle\_initial],  
[last\_name, :last\_name]  
]  
end

and I have models/name.rb which has…  
class Name  
attr\_reader :first\_name, :middle\_initial, last\_name

def initialize(first\_name, middle\_initial, last\_name)  
@first\_name = first\_name  
@middle\_initial = middle\_initial  
@last\_name = last\_name  
end  
end

and when I try to view a page, I get an error…undefined local variable  
or method ‘first\_name’ for Client:Class

I’m clearly missing something.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 6:26am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/5 "2006-01-25T06:26:41Z")

</div>

This breaks the normal rules of db, but I just create a name field and  
add a  
before\_save event to concatenate the individual names into the full  
name.  
This makes everything simpler for no real cost other than some rules.  
Who  
lives by the rules anyways? 🙂

Before\_save :fix\_name

def fix\_name  
record.full\_name = “#{record.first\_name} #{record.last\_name}”  
end

Not pretty but makes a lot of things lots easier, like in my case,  
auto\_complete\_fields.

Bob

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 7:25am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/6 "2006-01-25T07:25:16Z")

</div>

On Wed, 2006-01-25 at 00:02 -0600, Steve L. wrote:

> Try client.name.first\_name?

* * *

that just generates new errors.

I’ve fixed one of my errors as listed below…and now I get a hashed  
value for the ‘Name’ field… like #Name:0xb7b4305c

it’s better than an error I guess but it isn’t what I expected.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 8:07am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/7 "2006-01-25T08:07:35Z")

</div>

I had to put both declarations in the client.rb file.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [January 25, 2006, 7:04am UTC](https://www.ruby-forum.com/t/join-fields-for-list-views/96975/8 "2006-01-25T07:04:11Z")

</div>

Try client.name.first\_name?
