-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
ActiveRecord Hstore bug: can't update a key in the hash #6127
Comments
I can reproduce this issue. |
I can also reproduce this issue. |
A quick workaround would be to call (in your example) Historically, this has always been a problem with |
@al2o3cr ya, I've been looking at this. I'd like to treat the hstore columns as serialized columns, but it's going to take a little refactoring. |
@tenderlove has there been any refactoring to date that would have helped this? |
Here is another version and this one does not break the API. It is based on feedback given by @pixeltrix https://github.com/neerajdotname/rails/compare/6127b I'll clean up the code a bit , add tests and will delete some code that is not needed regarding hstore etc if there is any interest in moving hstore to be on top of serialization. |
@neerjdotname Forgive me if this seems naive, but wouldn't it make more sense to specify using hstore in the database.yml (serialize using hstore), what would happen if that model was using sqlite instead? |
@bloodycelt sqlite does not support hstore. So if you are using sqlite you should not add this line in your model .
|
Bump. Any updates on this? |
Isn't it related to #12395 ? |
Pushed back another point release? |
Yes. 😞 |
For a quick fix, I added |
Bump. I just reproduced this bug in I tried using |
Yikes, this is a nasty one for anyone using hstore. Just got bit by this... Example of problematic code
Output:
Version info:
Column info for "tags":
|
@gdeglin - I'd recommend one change to the above: |
This is a monkey patch I have been using to make that a default for hstores and arrays: module Ext::ActiveRecord::Dirty
private
# Private: Overwrites the default implementation to include arrays and hstores
# in the list of attributes that should always be saved.
#
# Without this, arrays and stores don't get their values updated in
# the database.
#
# Relates to this issue: https://github.com/rails/rails/issues/6127
def keys_for_partial_write
super | self.class.columns.select do |c|
c.try(:array) || c.type == :hstore
end.map(&:name)
end
end
ActiveSupport.on_load :active_record do
include Ext::ActiveRecord::Dirty
end |
+1 This is an issue for me too! |
This is the current expected behavior. You need to call @joevandyk thank you for reporting. |
Just faced this. What is the final call: |
Btw, in Postgres, hstore allows you to change the column partially, eg.
|
@saurabhnanda we keep the current behavior until we have a working solution for serialized attributes. |
Came here from Stack Overflow: http://stackoverflow.com/questions/20251296/how-can-i-update-a-data-records-value-with-ruby-on-rails-4-0-1-postgresql-hstor I'm fine with the current behavior but I wish this was documented better. Does Rails 4 have a documentation page on Hstore related stuff? |
@chibicode I'm working on a PostgreSQL specific guide. See my current draft it does go into the details and will be updated in the future. We are also working on resolving the situation with serialized attributes. Once we finished that, we can use that same behavior for |
@senny wow, that's very useful. Thank you! |
For what it's worth, this behavior has been fixed as of #15674 and will be part of 4.2. |
If Rails < 4.2, then handle it ourselves by duping the hash. If Rails >= 4.2, then rely on the Rails fix: - rails/rails#15674 - rails/rails#6127
joevandyk@f2318b2 shows a failing test.
The text was updated successfully, but these errors were encountered: