Forum: Ruby on Rails join fields for list views

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Craig W. (Guest)
on 2006-01-25 05: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
rubyonrails.org wiki and haven't stumbled into an appropriate page.

Pointers to references would be greatly appreciated.

Thanks

Craig
Bruce B. (Guest)
on 2006-01-25 07:11
(Received via mailing list)
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
Craig W. (Guest)
on 2006-01-25 07: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
possible.

thanks

Craig
Bob S. (Guest)
on 2006-01-25 07: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
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
Craig W. (Guest)
on 2006-01-25 07:58
(Received via mailing list)
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
Steve L. (Guest)
on 2006-01-25 08:04
(Received via mailing list)
Try client.name.first_name?
Craig W. (Guest)
on 2006-01-25 08:25
(Received via mailing list)
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
Craig W. (Guest)
on 2006-01-25 09: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.