Problem when updating a polymorphic model (0.4.1 version)

Added by Philipe Farias 196 days ago

Hi Andrew,

One problem (big, for me) with classy-inheritance 0.4.1 (in Rails 2.1)...
The polymorphic model that's associated via depends_on (RealState depends_on Address as addressable) isn't updating (only, no problem creating).
I have a RealState form that has some Address fields and when I update through this form only the RealState attributes gets updated.

Some notes:
- RealState belongs to User
- RealState has one Address as addressable
- Profile belongs to User
- Profile has one Address as addressable (same problem here)

That's in the RealState model:
belongs_to  :user, :counter_cache => true
depends_on  :address, :attrs => [:street, :number, :city],
                      :as => :addressable, :dependent => :destroy
That's in my RealStatesController:
def update
  @real_state = @user.real_states.find(params[:id])

  respond_to do |format|
    if @real_state.update_attributes(params[:real_state])
      flash[:notice] = 'Real State updated.'
      format.html { redirect_to([@user, @real_state]) }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @real_state.errors, :status => :unprocessable_entity }
    end
  end
end
real_states edit.html.erb:
<% form_for([@user, @real_state]) do |f| %>
  <%= render :partial => "form", :locals => { :f => f } %>
  <p>
  <%=submit_tag "Update"%>
  </p>
<% end %>
real_states _form.html.erb (the address fields):
<fieldset id="place">
  <legend>Where is?</legend>
  <%= f.label :street, "Street<em>*</em>" %>
  <%= f.text_field :street %>
  <hr />
  <%= f.label :number, "Number<em>*</em>" %>
  <%= f.text_field :number %>
  <hr />
  <%= f.label :city, "City<em>*</em>" %>
  <%= f.select :city, Address::CITIES_OPTIONS %>
  <hr />
</fieldset>
That shows in the log when I try to update:
Processing RealStatesController#update (for 127.0.0.1 at 2008-06-23 23:52:32) [PUT]
  Session ID: BAh7CToPdXN1YXJpb19pZGkHOg5yZXR1cm5fdG8wIgpmbGFzaElDOidBY3Rp
b25Db250cm9sbGVyOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsAOgxj
c3JmX2lkIiVkNzlmZjAxMjgwNzc2MWVkMGFmNWViOTNmZmI0ODA0NQ==--834c3e7b9d35a9710197475b72463d8956465dac
  Parameters: {"commit"=>"Update", "authenticity_token"=>"a3c6971d32d59c4ff08967f8d4672d61553336e1", "_method"=>"put", "action"=>"update", "id"=>"5", "user_id"=>"2", "controller"=>"real_states", "real_state"=>{"street"=>"Center Street", "rooms"=>"4", "value"=>"1500.00", "bathrooms"=>"3", "city"=>"Itabuna", "type"=>"house", "number"=>"92"}}
  User Columns (0.002100)   SHOW FIELDS FROM `users`
  User Load (0.001219)   SELECT * FROM `users` WHERE (`users`.`id` = 2) 
  User Load (0.001176)   SELECT * FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
  RealState Columns (0.003149)   SHOW FIELDS FROM `real_states`
  RealState Load (0.001742)   SELECT * FROM `real_states` WHERE (`real_states`.`id` = 5 AND (`real_states`.user_id = 2)) 
  Address Load (0.001472)   SELECT * FROM `addresses` WHERE (`addresses`.addressable_id = 5 AND (addressable_type = 'RealState')) LIMIT 1
  Address Columns (0.002648)   SHOW FIELDS FROM `addresses`
  SQL (0.000272)   BEGIN
  SQL (0.000265)   COMMIT
Redirected to http://localhost:3000/users/2/real_states/5
Completed in 0.09909 (10 reqs/sec) | DB: 0.01957 (19%) | 302 Found [http://localhost/users/2/real_states/5]

Look at the BEGIN COMMIT part...nothing...and in this case I have tried to change the street attribute (same for all others).

Hope you can shed some light in here...

Philipe Farias


Replies

RE: Problem when updating a polymorphic model (0.4.1 version) - Added by Andrew Stone 196 days ago

Philipe,

Apologies, there was an error with the 0.4.1 release and has been corrected, version 0.4.2 is now out. I incorrectly assumed (first mistake) that since Rails was automatically doing the create, it would do the updates. Well, thanks to you, I found out that's not true.

This was a quick release that shouldn't have been made. I will take the time to test more on future release to try an avoid these kinds of mistakes.

thanks,
andy