Ruby on rails 3 如何将时间戳字段设置为";更新;或;更新“全部”;ActiveRecord的方法?

Ruby on rails 3 如何将时间戳字段设置为";更新;或;更新“全部”;ActiveRecord的方法?,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,这是一个Rails3项目 我就是不知道怎么做。我有两个专栏: last_migrated timestamp migration_message text 我想更新的,附带条件: :subdomain => "someval" 但我尝试过的一切都失败了。我试过了MyModel.update和MyModel.update的每一个版本,我能想到的所有变体。由于某种原因,文件并没有穿透我厚厚的脑袋。请帮忙 为了澄清,我想使用AR方法执行以下操作: update brands set last

这是一个Rails3项目

我就是不知道怎么做。我有两个专栏:

last_migrated timestamp
migration_message text
我想更新的,附带条件:

:subdomain => "someval"
但我尝试过的一切都失败了。我试过了MyModel.update和MyModel.update的每一个版本,我能想到的所有变体。由于某种原因,文件并没有穿透我厚厚的脑袋。请帮忙

为了澄清,我想使用AR方法执行以下操作:

update brands
set last_migrated = x,
    migration_message = y
where subdomain = 'someval'

问题不清楚,但如果您试图更新模型的属性,请使用:

ModelName.update_attributes!(:migration_message => "The message", :last_migrated => the_timestamp) if ModelName.subdomain == "someval"
其中,_timestamp是日期时间对象或日期对象,具体取决于您在迁移中如何定义它

有关所有用途的更新信息:

ModelName.where(<condition>).update_all(:migration_message => "The message", :last_migrated => Time.now)
ModelName.where().update_all(:migration_message=>“The message”,:last_migrated=>Time.now)

问题不清楚,但如果您试图更新模型的属性,请使用:

ModelName.update_attributes!(:migration_message => "The message", :last_migrated => the_timestamp) if ModelName.subdomain == "someval"
其中,_timestamp是日期时间对象或日期对象,具体取决于您在迁移中如何定义它

有关所有用途的更新信息:

ModelName.where(<condition>).update_all(:migration_message => "The message", :last_migrated => Time.now)
ModelName.where().update_all(:migration_message=>“The message”,:last_migrated=>Time.now)
我最后做了:

  ActiveRecord::Base.connection.execute("update brands set migration_status = 'COMPLETE', migration_message = '', last_migrated = sysdate() where subdomain = '#{self.subdomain}'")
在AR中,这不是最好的方法,也不是Rails方法,但是我在AR中尝试的其他方法没有表现出来(全部更新),所以至少这是可行的

既然没有人能回答我的问题(谢谢你尝试amit),而且这个方法很有效,那么在有人能提供更好的答案之前,它将一直是我的答案。

我最后做了:

  ActiveRecord::Base.connection.execute("update brands set migration_status = 'COMPLETE', migration_message = '', last_migrated = sysdate() where subdomain = '#{self.subdomain}'")
在AR中,这不是最好的方法,也不是Rails方法,但是我在AR中尝试的其他方法没有表现出来(全部更新),所以至少这是可行的


既然没有人能回答我的问题(谢谢你尝试amit),而且这个方法很有效,那么它将一直是答案,直到有人能提供更好的答案。

你可以使用
where
这样的方法:

Brand.where(:subdomain => 'someval').update_all(:last_migrated => Time.now, :migration_message => 'Some message')

您可以像这样使用
where
方法:

Brand.where(:subdomain => 'someval').update_all(:last_migrated => Time.now, :migration_message => 'Some message')

假设子域是模型的另一个属性,编辑了答案。如果是其他内容,则以某种方式访问它并以类似方式添加到If条件。我正在寻找“update_all”类型的解决方案,这样我就不必遍历数千条记录。如果要使用相同的消息和时间戳更新所有记录,请尝试:ModelName.update_all(“migration_message='The message',last_migrated='The timestamp')这里的完整文档:谢谢,这是我读的第一件事。不幸的是,我在这里处理的是更新时间戳字段,您不能使用Time.now,在非符号更新字段中没有某种格式,例如“last_migrated='{Time.now}”“不行。文档中没有更新的例子,比如::migrated_at=>Time。现在,当您有多个列要更新时,再加上一个条件。编辑答案时,假设subdomain是模型的另一个属性。如果是其他内容,则以某种方式访问它并以类似方式添加到If条件。我正在寻找“update_all”类型的解决方案,这样我就不必遍历数千条记录。如果要使用相同的消息和时间戳更新所有记录,请尝试:ModelName.update_all(“migration_message='The message',last_migrated='The timestamp')这里的完整文档:谢谢,这是我读的第一件事。不幸的是,我在这里处理的是更新时间戳字段,您不能使用Time.now,在非符号更新字段中没有某种格式,例如“last_migrated='{Time.now}”“不行。文档中没有更新的例子,比如::migrated_at=>Time.now,当您有多个列要更新时,再加上一个条件。