Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 是否有方法获取关联模型的最新更新?_Ruby On Rails - Fatal编程技术网

Ruby on rails 是否有方法获取关联模型的最新更新?

Ruby on rails 是否有方法获取关联模型的最新更新?,ruby-on-rails,Ruby On Rails,我有汽车车型,它与许多其他车型相关。我如何查看所有关联模型的更新版本并获取最新版本,例如: 车门在“此时”更新 我的模型中有很多关联,因此获取它们中的每一个并进行比较是没有效率的。如果有更好的办法,请告诉我。谢谢。您可以使用这里的方法。基本上,触摸用于更新记录的字段中的updated\u。例如,Car.last.touch会将上次Car记录的updated_设置为当前时间 但是,touch也可以与关系一起使用,以触发关联对象上的touch方法。因此,在您的情况下,类似的方法可能会起作用: cla

我有
汽车
车型,它与许多其他车型相关。我如何查看所有关联模型的更新版本并获取最新版本,例如:

车门在“此时”更新

我的模型中有很多关联,因此获取它们中的每一个并进行比较是没有效率的。如果有更好的办法,请告诉我。谢谢。

您可以使用这里的方法。基本上,触摸用于更新记录的字段中的updated\u。例如,
Car.last.touch
会将上次
Car
记录的
updated_设置为当前时间
但是,
touch
也可以与关系一起使用,以触发关联对象上的
touch
方法。因此,在您的情况下,类似的方法可能会起作用:

class Car < ActiveRecord::Base
  belongs_to :corporation, touch: true
end

class Door < ActiveRecord::Base
  belongs_to :car, touch: true
end

# Door updation triggers updated_at of parent as well
@door = Door.last
@door.updated_at = DateTime.now
@door.save! # Updates updated_at of corresponding car record as well
class-Car
在上面的示例中,
@door.touch
也可用于更新相应父
汽车
记录的
更新位置。

您可以在此处使用此方法。基本上,触摸用于更新记录的
字段中的
updated\u。例如,
Car.last.touch
会将上次
Car
记录的
updated_设置为当前时间
但是,
touch
也可以与关系一起使用,以触发关联对象上的
touch
方法。因此,在您的情况下,类似的方法可能会起作用:

class Car < ActiveRecord::Base
  belongs_to :corporation, touch: true
end

class Door < ActiveRecord::Base
  belongs_to :car, touch: true
end

# Door updation triggers updated_at of parent as well
@door = Door.last
@door.updated_at = DateTime.now
@door.save! # Updates updated_at of corresponding car record as well
class-Car

在上面的例子中,
@door.touch
也可以用来更新相应的父
Car
记录的
updated\u。

答案只是针对一个关系,我有大约16个关系,正如我前面提到的,检查每个关系,然后进行比较是没有效率的。如果有更好的方法的话,我更愿意这样做。答案只是针对一个关系,我有大约16个关系,正如我上面提到的,检查每一个关系,然后进行比较是没有效率的。如果有更好的方法,我更愿意这样做。谢谢,这真的帮了大忙。谢谢,这真的帮了大忙。