0

I have a Property model with a has_many and accepts_nested_attributes_for on a Image model which uses attachment_fu. Updating a Property using the following code results in a database UPDATE for every single image (and each image thumbnail) no matter whether there were changes to it or not.

properties_controller.rb

  def update
    @property.update_attributes params[:property]
    redirect_to edit_property_path(@property)
  end

_form.html.erb

<% form_for @property do |property| %>  
  ...
  <ul id='image-admin'>
    <% @property.images.each do |image| %>
      <li>
      <%= image_tag image.public_filename(:front), :alt=> h(image.caption), :size => "218x160" %>
      <% property.fields_for :images, image do |img| %>
        <%= img.hidden_field :ordering, :class => 'order' %>
        <%= img.text_field :caption %>
        <span class='img_remove'>
          Remove ? <%= img.check_box '_delete' %>
        </span>
      <% end %>
      </li>
    <% end %>
  </ul>
  ...
<% end %>

script/server output

  SQL (8.7ms)   COMMIT
  SQL (0.1ms)   BEGIN
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 103402 WHERE `id` = 350
  Image Load (0.5ms)   SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'small' AND `images`.`parent_id` = 350) ORDER BY ordering ASC LIMIT 1
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 60535 WHERE `id` = 352
  Image Load (0.5ms)   SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'front' AND `images`.`parent_id` = 350) ORDER BY ordering ASC LIMIT 1
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 39888 WHERE `id` = 353
  Image Load (0.4ms)   SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'thumb' AND `images`.`parent_id` = 350) ORDER BY ordering ASC LIMIT 1
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 3510 WHERE `id` = 351
  SQL (0.9ms)   COMMIT
  SQL (0.1ms)   BEGIN
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 100387 WHERE `id` = 338
  Image Load (0.4ms)   SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'small' AND `images`.`parent_id` = 338) ORDER BY ordering ASC LIMIT 1
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:19', `size` = 58212 WHERE `id` = 340
  Image Load (0.4ms)   SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'front' AND `images`.`parent_id` = 338) ORDER BY ordering ASC LIMIT 1
  Image Update (0.8ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:20', `size` = 38101 WHERE `id` = 341
  Image Load (0.4ms)   SELECT * FROM `images` WHERE (`images`.`thumbnail` = 'thumb' AND `images`.`parent_id` = 338) ORDER BY ordering ASC LIMIT 1
  Image Update (0.3ms)   UPDATE `images` SET `updated_at` = '2009-09-01 15:17:20', `size` = 3241 WHERE `id` = 339
  SQL (0.8ms)   COMMIT

Any ideas why (and how I can stop) attachment_fu from doing this ? It looks like it thinks that the size attribute has changed but I can't see any reason (in my code, or in attachment_fu) why it should think that.

Marc
  • 483
  • 2
  • 13
  • Running the following from the rails console produces the same result, the image and its thumbnails are updated, but only the size and updated_at fields are set: i=Image.last; i.caption=i.caption; i.save; – Marc Sep 01 '09 at 17:49

1 Answers1

0

fields_for repeats the enclosed fields for every image in the association. But you have an outer loop that repeats fields_for for every image in the association. As a result when you submit you're sending a lot more updates than you want to be sending.

Try this:

<% form_for @property do |property| %>  
  ...
  <ul id='image-admin'>
    <% property.fields_for :images, image do |img| %>
      <li>
      <%= image_tag img.object.public_filename(:front), :alt=> h(img.object.caption), :size => "218x160" %>
        <%= img.hidden_field :ordering, :class => 'order' %>
        <%= img.text_field :caption %>
        <span class='img_remove'>
          Remove ? <%= img.check_box '_delete' %>
        </span>
      </li>
    <% end %>
  </ul>
  ...
<% end %>
Sarah Mei
  • 18,154
  • 5
  • 45
  • 45
  • The second parameter to fields_for takes the instance to create the fields for, which was provided by the .each loop in the 'image' variable. It was creating the correct markup, but if I enter no changes in the form (which is only for changing captions and deleting) and submit it, it updates every single image setting the updated_at and size columns. – Marc Sep 01 '09 at 17:38