Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Hstore return hstore column as a string when used inside a select + join #121

Open
pinouchon opened this issue Mar 28, 2014 · 0 comments
Open

Comments

@pinouchon
Copy link

I have Alert and AlertSub. AlertSub has_many :alerts.

[56] pry(main)> Alert.first.data
  Alert Load (0.6ms)  SELECT "alerts".* FROM "alerts" LIMIT 1
=> {"toto"=>"tata"} # type hash, ok
[57] pry(main)> Alert.select('data').all.first.data
  Alert Load (0.4ms)  SELECT data FROM "alerts"
=> {"toto"=>"tata"} # type hash, ok
[58] pry(main)> Alert.select('data as d').all.first.d
  Alert Load (0.5ms)  SELECT data as d FROM "alerts"
=> "\"toto\"=>\"tata\"" # string, not ok
                                 # .class says String
[61] pry(main)> AlertSub.joins(:alerts). # :alerts is just a has_many :alerts in class AlertSub
[61] pry(main)* select('alerts.id, alerts.data').
[61] pry(main)* where('alert_subs.user_id = 1').all.first.data
  AlertSub Load (0.7ms)  SELECT alerts.id, alerts.created_at, alerts.param, alerts.data::hstore FROM "alert_subs" INNER JOIN "alerts" ON "alerts"."alert_sub_id" = "alert_subs"."id" WHERE (alert_subs.user_id = 1)
=> "\"toto\"=>\"tata\"" # string, not ok

It looks like when I use the method .select with has, or with a join, I get back a string instead of a hash for the hstore value.

In my model, I do have the line: serialize :data, ActiveRecord::Coders::Hstore

How can I get back a hash, event when I use a .select ?

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

No branches or pull requests

1 participant