Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

difficult to persist after making changes to part of a hash attribute #8

Closed
stepheneb opened this issue Sep 20, 2017 · 1 comment
Closed

Comments

@stepheneb
Copy link

I'm persisting a hash attribute and I would like to make changes to parts of the hash and persist them -- however making changes to just part of a hash doesn't mark the model as changed.

The only way I found to do this so far is to serialize and un-serialize with json -- which decreases performance.

I am looking at the Modis code and it seems it's not responsible for maintaining the changed state.

A simple example of the issue:

class Book
  include Modis::Model
  attribute :pages, :integer
  attribute :desc, :hash
end


b1 = Book.create!(:pages => 100, :desc => { :a => 'good' })
=> #<Book:0x007ffdd7f74bd0

b1.pages = 99
=> 99

b1.changed?
=> true

b1.save
=> true

b1.changed?
=> false

b1.desc = { :a => 'very good' }
=> {:a=>"very good"}

b1.changed?
=> true

b1.save
=> true

b1.changed?
=> false

b1.desc[:a] = 'super good'
=> "super good"

b1.desc
=> {:a=>"super good"}

b1.changed?
=> false

b1.desc = JSON(b1.desc.to_json)
=> {"a"=>"super good"}

b1.changed?
=> true
@stepheneb
Copy link
Author

Ahh ... looking into ActiveModel::Dirty found the solution.

Continuing on from the example above:

b1.save
=> true

b1.changed?
=> false

b1.desc_will_change!
=> {"a"=>"super good"}

b1.desc_change
=> [{"a"=>"super good"}, {"a"=>"super good"}]

b1.desc["a"] = 'mega good'
=> "mega good"

b1.desc_change
=> [{"a"=>"super good"}, {"a"=>"mega good"}]

b1.changed?
=> true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant