为什么这些数组操作不能保存在ActiveRecord中?

时间:2015-06-05 03:52:57

标签: ruby-on-rails arrays postgresql ruby-on-rails-4 activerecord

当我在我的控制台中执行此操作时,它不会将值保存在我的记录中:

[87] pry(main)> Node.with_tagged_users.each do |node|
[87] pry(main)*   tagged_users = node.tagged_users  
[87] pry(main)*   node.cached_tagged_user_names << tagged_users.map(&:name)  
[87] pry(main)*   node.save!
[87] pry(main)* end  
  Node Load (0.5ms)  SELECT "nodes".* FROM "nodes"  WHERE (cached_num_user_tags > 0)
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'gerry@test.com' LIMIT 1
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'danny@test.com' LIMIT 1
   (0.1ms)  BEGIN
  ActsAsTaggableOn::Tag Load (1.9ms)  SELECT "tags".* FROM "tags"  WHERE (LOWER(name) = LOWER('gerry@test.com') OR LOWER(name) = LOWER('danny@test.com'))
  ActsAsTaggableOn::Tag Load (0.9ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'user_tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 85], ["taggable_type", "Node"]]
   (0.1ms)  COMMIT
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'gerry@test.com' LIMIT 1
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'abc@test.com' LIMIT 1
   (0.1ms)  BEGIN
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags"  WHERE (LOWER(name) = LOWER('gerry@test.com') OR LOWER(name) = LOWER('abc@test.com'))
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'user_tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 86], ["taggable_type", "Node"]]
   (0.1ms)  COMMIT
=> [#<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-06-03 00:10:17", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, danny@test.com", cached_num_user_tags: 2, cached_tagged_user_names: [["Gerry  Atrick", "Daniel Marty"]]>,
 #<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-03 00:37:56", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: [["Gerry  Atrick", "Marcamus Prime"]]>]
[88] pry(main)> Node.last
  Node Load (0.5ms)  SELECT  "nodes".* FROM "nodes"   ORDER BY "nodes"."id" DESC LIMIT 1
=> #<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-03 00:37:56", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: []>

请注意,在COMMIT之后,如果查看Node对象的最后一个属性/列,它就有一个如下数组的数组:cached_tagged_user_names: [["Gerry Atrick", "Marcamus Prime"]]>]这是正确的。

但是,一旦我通过Node.last在控制台中重新加载该对象,如果你查看最后一列,它就是空的。即使每个其他列都与看似成功的版本相同。

这对于所有nodes都是一致的,在这种情况下只是2。

可能导致此值无法正确保存的原因是什么?

这是我db/schema.rb中与该值对应的列:

t.string   "cached_tagged_user_names", default: [],  array: true

修改1

我也尝试过node.save而且也没有用。

修改2

如果我手动将元素一次添加到该属性的任何记录中,它会正确保存。 E.g:

[92] pry(main)> g
=> "gerry@test.com"
[93] pry(main)> m.cached_tagged_user_names << g
=> ["gerry@test.com"]
[94] pry(main)> m
=> #<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-03 00:37:56", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["gerry@test.com"]>
[95] pry(main)> m.save
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
=> true
[96] pry(main)> m
=> #<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-03 00:37:56", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["gerry@test.com"]>

[97] pry(main)> p = "Gerry Atrick"
=> "Gerry Atrick"
[98] pry(main)> m.cached_tagged_user_names << p
=> ["gerry@test.com", "Gerry Atrick"]
[99] pry(main)> m
=> #<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-03 00:37:56", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["gerry@test.com", "Gerry Atrick"]>
[100] pry(main)> m.save
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
=> true
[101] pry(main)> m
=> #<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-03 00:37:56", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["gerry@test.com", "Gerry Atrick"]>

唯一突然出现的是,对于第二组值,它只是一个数组......而不是一个数组数组。但我认为Rails应该完美地处理这个问题,还是我错了?

0 个答案:

没有答案