I have data stored in a database like this
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
condition: between_number
value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
But I have to display it like this in laravel
{"accepted"=>{"last_order_total"=>{"condition"=>"between_number", "value"=>"1000&2000"}, "last_order_date"=>{"condition"=>"before", "value"=>"2021-10-01T00:00:00.000Z"}, "number_of_order"=>{"condition"=>"between_number", "value"=>"10&20"}}, "removed"=>{"last_order_date"=>{"condition"=>"after", "value"=>"2021-10-14T00:00:00.000Z"}, "last_order_total"=>{"condition"=>"between_number", "value"=>"1200&1500"}}}
so how can convert database values like this? And I'm using Postgresql to store data.
CodePudding user response:
Assuming you're unable to reformat data in the database, and that running a ruby application to fetch this data is not practical, I would suggest stripping any mention of !ruby/hash:ActiveSupport::HashWithIndifferentAccess
from the value after reading it:
- Fetch the YAML (string) from the database.
- Strip
!ruby/hash:ActiveSupport::HashWithIndifferentAccess
from the string. - Parse the result as regular YAML
- Display it as you wish.