Reputation: 95
I am developing the application with Ruby on Rail. As in my current project I have an active record model as an example below :
<Site id: 6241, collection_id: 7, name: "ABC", properties: {"31"=>20}>
<Site id: 6242, collection_id: 7, name: "ABC", properties: {"10"=>20}>
<Site id: 6243, collection_id: 7, name: "ABC", properties: {"11"=>30}>
However, I want to find the site which has the properties with the key '31' and value 20, but I cannot find the way to compare this hash attribute. So is there any way you could suggest me?
Upvotes: 2
Views: 3974
Reputation: 10326
Also assuming your column type is an hstore, you can just:
Site.where(properties: { key: "value" })
Upvotes: 3
Reputation: 317
I also use mysql and this will work:
Site.where(properties: {"31"=>20}.to_yaml)
Upvotes: 0
Reputation: 44360
If column type hstore
and db postgres
you can search by this query:
Site.where("properties @> hstore(?, ?)", 'key', 'value')
Upvotes: 1