has one assciation insert record question

Valentino Lun
Thu, 11 Jun 2009 08:53:32 -0000

Dear all

I have two model, Event and EventDetail

class Event < ActiveRecord::Base
  set_table_name "ssc_event"
  set_primary_key "event_id"
  has_one :event_detail, :class_name => ''EventDetail'',
:foreign_key =>
''event_id''
end

class EventDetail < ActiveRecord::Base
    set_table_name ''ssc_event_detail''
    set_primary_key "event_id"
    belongs_to :event, :class_name => ''Event'', :foreign_key
=>
''event_id''
end

The schema in ssc_event.event_id is an identity column.
The schema in ssc_event_detail.event_id is an integer column

I use the following ruby code to insert a new record, but I think it is
not clever.

a = Event.new
a.cluster_code = "KWC"
a.hosp_code = "CMC"
a.save
b = EventDetail.new
b.id = a.id.to_i
b.detail = "This is detail"

Any one have a better solution? Please share.

Many thanks
Valentino
--
Posted via http://www.ruby-forum.com/.