I have a pair of models:
class Document < ActiveRecord::Base
# attributes: Title, Number, etc...
Class Revision < ActiveRecord::Base
# attributes: document_id, revision, release_status, etc..
I have a controller & view for the Revision model (note, this is the
model that belongs to the Document model).
the "edit" view looks something like this:
<% form_for(@revision) do |f| %>
<% fields_for(@revision.number) do |r| %>
<%= r.text_field :title %>
<%= r.text_field :number %>
When my "update" method gets called, how can I update the
corresponding "revision.number" record simultaneously with updating my
I know I can do something like this:
success = @revision.number.update_attributes(params[:number])
success &= @revision.update_attributes(params[:revision])
but, what happens if the second call to "update_attributes" fails? I
will have updated the Number model, but not the Revision model.
Is there an idiomatic way to handle this?
I suppose I could rethink my application, and write a documents
controller & view and update the revision as part of updating the
document. But I would like to know how to update the parent of a
belongs_to association regardless.
Thanks for your advice and enlightenment.