I just noticed that one of the attributes of an object was being updated when this object was appended to an array. This behavior looks very surprising to me and I thought I might be missing something fundamental about ActiveRecord.
我刚刚注意到,当此对象附加到数组时,正在更新对象的一个属性。这种行为对我来说非常令人惊讶,我想我可能会遗漏一些关于ActiveRecord的基本信息。
In my app, every idea has a father (through its father_id attribute). This association is set in models/idea.rb with the following :
在我的应用程序中,每个想法都有一个父(通过其father_id属性)。此关联在models / idea.rb中设置,具有以下内容:
class Idea < ActiveRecord::Base
belongs_to :father, :class_name => "Idea" # , :foreign_key => "idea_id"
has_many :children, :class_name => "Idea", :foreign_key => "father_id", :dependent => :destroy
[...]
Here is what happens in rails console :
以下是rails控制台中发生的情况:
I first select a given idea :
我首先选择一个给定的想法:
irb(main):003:0> n = Idea.find(1492)
Idea Load (1.1ms) SELECT "ideas".* FROM "ideas" WHERE "ideas"."id" = $1 LIMIT 1 [["id", 1492]]
=> #<Idea id: 1492, father_id: 1407, [...]>
I then retrieve its children through the associations :
然后我通过协会检索它的孩子:
irb(main):004:0> c = n.children
Idea Load (0.5ms) SELECT "ideas".* FROM "ideas" WHERE "ideas"."father_id" = 1492
=> []
It doesn't have any, which is ok. I then want to append the idea itself to the 'c' variable but this triggers an unwanted UPDATE action in the database :
它没有任何,这没关系。然后我想将这个想法本身附加到'c'变量,但这会在数据库中触发不需要的UPDATE操作:
irb(main):005:0> c << n
(0.1ms) BEGIN
(0.9ms) UPDATE "ideas" SET "father_id" = 1492, "updated_at" = '2013-12-06 12:57:25.982619' WHERE "ideas"."id" = 1492
(0.4ms) COMMIT
=> [#<Idea id: 1492, father_id: 1492, [...]>]
The father_id attribute, which had the value 1407 now has the value 1492, i.e. the idea's id.
具有值1407的father_id属性现在具有值1492,即构思的id。
Can anyone explain me why this happens and how I can create an array that includes an object's children and the object itself without altering the object's attributes ?
任何人都可以解释为什么会发生这种情况以及我如何创建一个包含对象子节点和对象本身的数组而不改变对象的属性?
NB : I'm using ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin13.0.0]
注意:我使用的是ruby 1.9.3p448(2013-06-27修订版41675)[x86_64-darwin13.0.0]
1 个解决方案
#1
1
This is expected behavior. You're adding a new idea to the set of ideas belonging to a specific father. This happens because it's not an array you're appending to, it's an ActiveRecord association. In your console, try n.children.class
.
这是预期的行为。您正在为属于特定父亲的一组想法添加新想法。发生这种情况是因为它不是你要附加的数组,它是一个ActiveRecord关联。在您的控制台中,尝试n.children.class。
If you want a flat array which won't modify objects appended to it, you want:
如果你想要一个不会修改附加到它的对象的平面数组,你需要:
c = n.children.to_a
c << n
#1
1
This is expected behavior. You're adding a new idea to the set of ideas belonging to a specific father. This happens because it's not an array you're appending to, it's an ActiveRecord association. In your console, try n.children.class
.
这是预期的行为。您正在为属于特定父亲的一组想法添加新想法。发生这种情况是因为它不是你要附加的数组,它是一个ActiveRecord关联。在您的控制台中,尝试n.children.class。
If you want a flat array which won't modify objects appended to it, you want:
如果你想要一个不会修改附加到它的对象的平面数组,你需要:
c = n.children.to_a
c << n