Join fields for list views


#1

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 wiki and haven’t stumbled into an appropriate page.

Pointers to references would be greatly appreciated.

Thanks

Craig


#2

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


#3

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


#4

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


#5

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? :slight_smile:

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


#6

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


#7

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


#8

Try client.name.first_name?