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
Smith")
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
Smith")
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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---