Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 ActiveRecord是否覆盖在不同时间加载的对象的更改?_Ruby_Activerecord - Fatal编程技术网

Ruby ActiveRecord是否覆盖在不同时间加载的对象的更改?

Ruby ActiveRecord是否覆盖在不同时间加载的对象的更改?,ruby,activerecord,Ruby,Activerecord,我在Ruby中使用ActiveRecord,没有使用Rails。如果我使用以下代码,第一次对属性所做的更改会覆盖第二次对属性所做的更改吗。第一个和第二个都是相同的记录 def my_method first = MyClass.find(1) first.my_attribute = "I am an attribute" first.save end second = MyClass.find(1) second.my_attribute = "this is my attrib

我在Ruby中使用ActiveRecord,没有使用Rails。如果我使用以下代码,第一次对属性所做的更改会覆盖第二次对属性所做的更改吗。第一个和第二个都是相同的记录

def my_method
  first = MyClass.find(1)
  first.my_attribute = "I am an attribute"
  first.save
end

second = MyClass.find(1)
second.my_attribute = "this is my attribute"
my_method
second.save

活动记录没有标识映射:虽然
第一个
第二个
表示数据库中的同一行,但这两个内存中的对象是分开的,彼此不知道

my_方法中保存
将完成,然后第二次保存将覆盖这些更改

如果你想检测到这类事情,一种方法是。这允许您检测何时保存过时对象:第二次保存将引发异常。您可以在类中通过添加一个名为
lock\u version
的整数列来激活它(确保其默认值为0)