New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Touch method to ActiveRelation #8343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -519,7 +519,33 @@ def to_sql | ||
end | end | ||
end | end | ||
|
|
||
# Returns a hash of where conditions. | # Touches multiple records. | ||
# | |||
# === Examples | |||
# | |||
# # Touch all records | |||
# Person.all.touch | |||
# # => "UPDATE \"persons\" SET \"updated_at\" = '2063-04-04 22:55:23.132670'" | |||
# | |||
# # Touch multiple records with a custom attribute | |||
# Person.all.touch(:created_at) | |||
# # => "UPDATE \"persons\" SET \"updated_at\" = '2012-11-27 23:12:25.745411', \"created_at\" = '2012-11-27 23:12:25.745411'" | |||
# | |||
# # Touch records with scope | |||
# Person.where(:name => 'David').touch | |||
# # => "UPDATE \"persons\" SET \"updated_at\" = '2012-11-27 23:12:30.087110' WHERE \"persons\".\"name\" = 'David'" | |||
def touch(name = nil) | |||
attributes = [:updated_at] | |||
attributes << name if name | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This always sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behavior of this method closely follows the behavior of The only caveat is that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. That feels weird to me too :) |
|||
now = klass.default_timezone == :utc ? Time.now.utc : Time.now | |||
value_hash = attributes.inject({}) do |values, column| | |||
values[column] = now | |||
values | |||
end | |||
update_all(value_hash) | |||
end | |||
|
|||
# Returns a hash of where conditions | |||
# | # | ||
# User.where(name: 'Oscar').where_values_hash | # User.where(name: 'Oscar').where_values_hash | ||
# # => {name: "Oscar"} | # # => {name: "Oscar"} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be called
touch_all
, for consistency withupdate_all
,destroy_all
, etc.?