On Aug 28, 1:39 pm, Jonathan Stott <[email protected]> wrote:
> On 28 August 2010 17:01, dev2 <[email protected]> wrote:
>
> > When I automigrate these models I don't get the results I expect.
> > After migrating the person table now has included the fields from the
> > doctor table which seems incorrect to me as most of the entries in the
> > person table won't have 'doctor' properties.   My purpose of using STI
> > is to make it easier to create/update a doctor person.   The way I do
> > it now I save the person data and if that succeeds then I create an
> > entry in the doctor table with the corresponding person_id.   Any
> > help?
>
> STI is Single Table Inheritance.  Note the single in the name :)
>
> For the data layout you want, you could have the Doctor model belong_to
> :person.  (which actually, looking at it, you have the property there
> already).  For saving models, if you wrap in a transaction, that should solve
> potential for inconsistency issues.
>
> require 'dm-transactions'
>
> Person.transaction do
>   if @person.save
>     @doctor = Doctor.new
>     @doctor.person = @person
>     unless @doctor.save
>       raise "Transaction failed" # rollback transaction
>     end
>   else
>     raise "Transaction failed" # rollback transaction
>   end
> end
>
> Regards
> Jon

So to make life easy I would need to change the field names to match
the table they came from.  (ie experience would be named
doctor[experience] and name would be named person[name])

<h1>Doctors controller, new action</h1>
<%= form_for(:doctor, :action=>url(:doctor, :create)) do %>
  <%= text_field :name, :label => 'Name:' %>

  <%= text_field :experience, :label => 'Experience:' %>
  <%= submit 'Create' %>
<% end =%>

 require 'dm-transactions'

@person = Person.new(params[:person])
 Person.transaction do
   if @person.save
     @doctor = Doctor.new(params[:doctor])    #...
     @doctor.person = @person
     unless @doctor.save
       raise "Transaction failed" # rollback transaction
     end
   else
     raise "Transaction failed" # rollback transaction
   end
 end

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to