views:

37

answers:

1

Hello, coming from my experience with nHibernate, I wonder how one maps (n)hibernate's component classes in ActiveRecord

class Person < ActiveRecord::Base
   #somehow get a :name attribute here
end

class Name
  @fist_name
  @last_name
end

how can this be done with one table only (so this is no 1:1, but i want to have a :name_first_name column in the db (or whatever the convention is)?

+2  A: 

That's what composed_of is for.

For You example:

class Person < ActiveRecord::Base
   composed_of :name, :class_name => "Name", :mapping =>
            [ # database                         ruby
              %w[ first_name    first_name ],
              %w[ last_name    last_name ]
            ],
end

class Name
  attr_accessor :first_name, :last_name
end

You'll then have to add two database columns ( first_name, last_name ).

Baju
Thanks a lot, this wil do it :)
Jan Limpens