当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - Ror Active Record "component" classes

问题描述:

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)?

网友答案:

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 ).

分享给朋友:
您可能感兴趣的文章:
随机阅读: