# One-to-one polymorphic rails model

**URL:** <https://www.ruby-forum.com/t/one-to-one-polymorphic-rails-model/132863>\
**Category:** Rails\
**Created:** [March 25, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/one-to-one-polymorphic-rails-model/132863 "2008-03-25T13:11:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Col\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Col\_W](https://www.ruby-forum.com/u/Col_W)\
**Post date:** [March 25, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/one-to-one-polymorphic-rails-model/132863/1 "2008-03-25T13:11:55Z")

</div>

Can someone help me work out what is best practice for initializing  
the objects in a one-to-one polymorphic rails model, or just explain  
whayt goes on in the background.

Let me give you a silly example model representing staff and students  
which all have canteen charge cards provided when they join.

class Staff \< ActiveRecord::Base  
has\_one :charge\_card, :as =\> :chargable  
end

class Student \< ActiveRecord::Base  
has\_one :charge\_card, :as =\> :chargable  
end

class ChargeCard \< ActiveRecord::Base  
belongs\_to :chargable, :polymorphic =\> true  
end

I would have thought that somewhere under the covers;

s = Staff.create( :ref =\> “ST72913”, :name =\> “John S.”)

would have created and saved to the database, not only the staffer,  
but also a default charge card object.

However the only way I can figure to do that is to do:

s = Staff.create( :ref =\> “ST72913”, :name =\> “John S.”)  
s.create\_charge\_card( :balance =\> 0 )

which is not quite the voodoo that I would have expected.

Can anyone explain what I’m doing wrong, or explain why I’m so stupid  
as to think that is the way it should work in the first place?

Groove on and thanks.
