5

I have 2 objects, Visitors and Events. Visitors have multiple Events. An event stores parameters like this...

#<Event id: 5466, event_type: "Visit", visitor_token: "c26a6098-64bb-4652-9aa0-e41c214f42cb", contact_id: 657, data: {"url"=>"http://widget.powerpress.co/", "title"=>"Home (light) | Widget"}, created_at: "2015-12-17 14:51:53", updated_at: "2015-12-17 14:51:53", website_id: 2>

As you can see, there is a serialized text column called data that stores a hash with more data.

I need to find out if a visitor has visited a certain page, which would be very simple if the url parameter were it's own column, or if the hash were an hstore column, however it wasn't originally set up that way and it's a part of the saved hash.

Here's my attempted rails queries...

visitor.events.where("data -> url = :value", value: 'http://widget.powerpress.co/')

visitor.events.where("data like ?", "{'url' => 'http://widget.powerpress.co/'}")

visitor.events.where("data -> :key LIKE :value", :key => 'url', :value => "%http://widget.powerpress.co/%")

How does one properly query postgres to find objects that have a hash that contains a key with a specific value?

1
  • the data column is a String ? Commented Dec 17, 2015 at 16:19

3 Answers 3

1

I suspect you're not looking for the right string. It should be "url"=>"http://widget.powerpress.co/", so:

visitor.events.where("data like ?", '%"url"=>"http://widget.powerpress.co/"%')

Check the right value directly in DB.

Sign up to request clarification or add additional context in comments.

3 Comments

While this runs it doesn't return anything (there are for sure events with that url btw). I can successfully run Event.where("data like ?", '%http://widget.powerpress.co/%') however this includes events with urls like /blog etc. Any work around?
Can you add to your post an actual value of the column data from the DB ? You posted an exemple from Rails, but the format may be different in DB.
Event.last.attributes_before_type_cast returns {"id"=>"5466", "event_type"=>"View", "visitor_token"=>"c26a6098-64bb-4652-9aa0-e41c214f42cb", "contact_id"=>"657", "data"=>{"url"=>"http://widget.powerpress.co/", "title"=>"Home (light) | Widget", "page"=>"/", "time"=>"1450363908.892", "contactable_name"=>"overlay theme 8"}, "created_at"=>"2015-12-17 14:51:53.9496", "updated_at"=>"2015-12-17 14:51:53.9496", "website_id"=>"2", "tag_id"=>nil, "contactable_type"=>"Overlay", "contactable_id"=>"87", "visitor_id"=>"117", "broadcast_id"=>nil, "flow_id"=>"7", "stage_id"=>nil, "goal_id"=>"5", "url"=>nil}
0

If you are storing hash in a text column, try following:

visitor.events.select{|ve| eval(ve.data)["url"] == "http://widget.powerpress.co/"}

Hope, it helps!

2 Comments

TypeError: no implicit conversion of Hash into String
Interesting. Please post the output of Event from rails console.
0

It worked for me.

 visitor.events.select { |n| n.data && n.data['url'] == "http://widget.powerpress.co/"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.