Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[Patch] ActiveRecord::Timestamp#touch still fails when no updated_at/on column is present

#5439

Hi,

ActiveRecord::Timestamp#touch generates malformed SQL when the models table misses an updated_at/on column.

For example, when I execute

  Game.first.touch

the following error is raised:


ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (
games.id = 1)' at line 1: UPDATE games SET WHERE (games.id = 1)

Shouldnt #touch in this case just return false instead of raising an error?

This is especially a problem with polymorphic associations, i.e.

  class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true, :touch => true
  end

does not work when one of the commentable models misses the updated_at/on column.

Regards
Lars

Reported by Lars Kuhnt · August 24th, 2010 @ 12:12 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Milestone set to 3.x
    • State changed from new to open
    • Assigned user set to Neeraj Singh
    • Importance changed from to Low

    August 24th, 2010 @ 02:46 PM

  2. Neeraj Singh
    Neeraj Singh
    • Assigned user changed from Neeraj Singh to José Valim
    • Tag changed from activerecord, timestamps, touch to activerecord, patch, timestamps, touch

    Attached is code with test patch.

    August 24th, 2010 @ 02:58 PM

  3. Repository
    Repository
    • State changed from open to resolved

    August 24th, 2010 @ 03:26 PM

  4. Repository
  5. Troels Petersen
    Troels Petersen

    The patch seems to do the opposite of what was intended:

    def touch(name = nil)

    attributes = timestamp_attributes_for_update_in_model
    unless attributes.blank?
      attributes << name if name
      ...
    

    For a model without an :updated_xx attribute, the attributes variable will be blank, so touch(:other_attribute) does nothing.

    I think you need to interchange the lines

    unless attributes.blank?

    attributes << name if name
    

    September 23rd, 2010 @ 07:20 PM

  6. Troels Petersen
    Troels Petersen
    • Title changed from ActiveRecord::Timestamp#touch fails when no updated_at/on column is present to [Patch] ActiveRecord::Timestamp#touch still fails when no updated_at/on column is present

    Here is a patch to restore the behavior of touch on an arbitrary (date or datetime) attribute.

    The point is that since touch has been generalized to update any given attribute, it should do so whether or not the model also has an updated_xx attribute.

    September 24th, 2010 @ 06:56 PM

  7. José Valim
    José Valim
    • Milestone cleared.
    • State changed from resolved to open

    I agree with the patch. I will apply it next week when I give another pass at LH.

    September 24th, 2010 @ 07:00 PM

  8. Repository
  9. Repository
  10. Jeremy Kemper