Content
View differences
Updated by Pavel Balashou about 3 years ago
`store_accessor` should have been used instead of `store` method.
Quote from [rails API](https://api.rubyonrails.org/classes/ActiveRecord/Store.html):
> NOTE: If you are using structured database data types (e.g. PostgreSQL `hstore`/`json`, or MySQL 5.7+ `json`) there is no need for the serialization provided by [.store](https://api.rubyonrails.org/classes/ActiveRecord/Store/ClassMethods.html#method-i-store). Simply use [.store\_accessor](https://api.rubyonrails.org/classes/ActiveRecord/Store/ClassMethods.html#method-i-store_accessor) instead to generate the accessor methods. Be aware that these columns use a string keyed hash and do not allow access using a symbol.
If the `store` is used with `coder` JSON or without `coder` parameter specified(YAML is default) then extra escape chars appear and it breaks the behavior of jsonb column(for example, query operators does not work):
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56369/content">
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56372/content">
If we just define `.store_accessor` then serialization works correctly and jsonb column behaves as expected:
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56370/content">
And the default value was set incorrectly. :
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56585/content">
Then it should should be:
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56587/content">
Quote from [rails API](https://api.rubyonrails.org/classes/ActiveRecord/Store.html):
> NOTE: If you are using structured database data types (e.g. PostgreSQL `hstore`/`json`, or MySQL 5.7+ `json`) there is no need for the serialization provided by [.store](https://api.rubyonrails.org/classes/ActiveRecord/Store/ClassMethods.html#method-i-store). Simply use [.store\_accessor](https://api.rubyonrails.org/classes/ActiveRecord/Store/ClassMethods.html#method-i-store_accessor) instead to generate the accessor methods. Be aware that these columns use a string keyed hash and do not allow access using a symbol.
If the `store` is used with `coder` JSON or without `coder` parameter specified(YAML is default) then extra escape chars appear and it breaks the behavior of jsonb column(for example, query operators does not work):
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56369/content">
If we just define `.store_accessor` then serialization works correctly and jsonb column behaves as expected:
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56370/content">
And the default value was set incorrectly. :
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56585/content">
Then it should should be:
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/56587/content">