# Has one assciation insert record question

**URL:** <https://www.ruby-forum.com/t/has-one-assciation-insert-record-question/170015>\
**Category:** Rails\
**Created:** [June 11, 2009, 10:53am UTC](https://www.ruby-forum.com/t/has-one-assciation-insert-record-question/170015 "2009-06-11T10:53:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 11, 2009, 10:53am UTC](https://www.ruby-forum.com/t/has-one-assciation-insert-record-question/170015/1 "2009-06-11T10:53:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 11, 2009, 11:18am UTC](https://www.ruby-forum.com/t/has-one-assciation-insert-record-question/170015/2 "2009-06-11T11:18:38Z")

</div>

On Jun 11, 9:53 am, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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”

with associations you get a.build\_event\_detail for free. The  
associations guide (

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

) has more details

Fred
