Adding an existing tag leaves the tag_names array out of sync with the DB
aschmied opened this issue · comments
Environment
- Rails 5.2.2
- JRuby
Steps
If you use +=
on tag_names
to add a tag that is already in tag_names
then the tag_names
array is out of sync with the DB:
irb(main):018:0> model.tag_names = ["tag1", "tag2"]
=> ["tag1", "tag2"]
irb(main):019:0> model.save!
=> true
irb(main):020:0> model.tag_names += ["tag2", "tag3"]
=> ["tag1", "tag2", "tag2", "tag3"]
irb(main):021:0> model.save!
=> true
irb(main):022:0> model.tag_names
=> ["tag1", "tag2", "tag2", "tag3"]
irb(main):023:0> model.reload
irb(main):024:0> model.tag_names
=> ["tag1", "tag2", "tag3"]
Possibly as designed. I worked around it by calling reload
.
Thanks for the quick turnaround! There is a related behaviour when you add a tag with upper-case letters:
irb(main):002:0> e.tag_names
=> ["tag1"]
irb(main):003:0> e.tag_names += ["Upper"]
=> ["tag1", "Upper"]
irb(main):004:0> e.tag_names
=> ["tag1", "Upper"]
irb(main):005:0> e.save!
=> true
irb(main):006:0> e.tag_names
=> ["tag1", "Upper"]
irb(main):007:0> e.reload
irb(main):008:0> e.tag_names
=> ["tag1", "upper"]
It looks like any time GutenTag is doing manipulation of the tag_names array before persisting it to the DB those changes are not synced back to the array.
Thanks Anthony - I've pushed a fix for this, and published a new release - 2.5.1 - which includes all of these changes :)