Forum: Ruby on Rails join fields for list views

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-25 04:49
(Received via mailing list)
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 wiki and haven't stumbled into an appropriate page.

Pointers to references would be greatly appreciated.


2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-25 06:11
(Received via mailing list)

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.

C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-25 06:20
(Received via mailing list)
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


Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-25 06:26
(Received via mailing list)
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
This makes everything simpler for no real cost other than some rules.
lives by the rules anyways? :)

Before_save :fix_name

def fix_name
  record.full_name = "#{record.first_name} #{record.last_name}"

Not pretty but makes a lot of things lots easier, like in my case,

C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-25 06:58
(Received via mailing list)
The Agile book isn't clear to me.

I have clients database...fields:

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 ]

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

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.

5ce55767684e0cd1727db2b8b8975640?d=identicon&s=25 Steve Longdo (Guest)
on 2006-01-25 07:04
(Received via mailing list)
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-25 07:25
(Received via mailing list)
On Wed, 2006-01-25 at 00:02 -0600, Steve Longdo wrote:
> Try
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.

C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-25 08:07
(Received via mailing list)
I had to put both declarations in the client.rb file.
This topic is locked and can not be replied to.