When I do this in my console, it doesn't save the value on my record:
[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: []>
Notice that right after the COMMIT
, if you look on the last attribute/column of the Node
object, it has an array of arrays like this: cached_tagged_user_names: [["Gerry Atrick", "Marcamus Prime"]]>]
which is correct.
However, once I reload that object in the console via Node.last
, if you look at the last column it is empty. Even though every other column is identical to the seemingly successful version.
This is consistent for all the nodes
, in this case just 2.
What could be causing this value not be saved correctly?
This is the column in my db/schema.rb
that corresponds to that value:
t.string "cached_tagged_user_names", default: [], array: true
Edit 1
I also tried just doing node.save
and that didn't work either.
Edit 2
If I manually add elements, one at a time, to any of the records for that attribute it does save properly. 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"]>
The only thing that jumps out at me is that for the 2nd set of values, it is simply just an array...not an array of arrays. But I thought Rails should handle this issue perfectly, or am I mistaken?