Request for Json data type in postgres Rails-4

I use Rails-4, have a product model and saved specifications as a JSon type

In the migration file add add_column :products, :specifications, :json

An example product record looks like

#<Product id: 1, prod_id: 525141, cat_id: 6716, category_id: 5, updated: "2013-09-24 07:37:20", created_at: "2014-03-07 12:21:34", updated_at: "2014-03-07 12:32:36", eans: ["4016032274001"], skus: ["DK-1511-010F/WH"], account_id: 2, specifications: {"network"=>["PCI-Express 2.1 16x", "CardBus", "PCI-Express 3.0 16x", "PCI 64-bit, 66MHz", "PCI 64-bit, 33MHz", "PCI 32-bit, 66MHz", "PCI 3.0", "PCI 2.3", "PCI 2.2", "PCI-X", "PCI-Express 16x", "PCI-Express 8x", "PCI-Express 4x", "PCI-Express 2.0 16x", "PCI-Express 1x", "PCI", "PC Card", "ISA", "AGP 8x", "AGP 4x", "AGP 2x", "AGP 1x"], "rating"=>[4]}>

I want to request the product Specification.eg: get all products whose rating (internal specifications) is 4.

Is there any gem to implement this?

+4
source share
2 answers

With the advent of jsonb in Rails 4.2 with Postgres 9.4, you can do this IF the evaluation field is a string, not an integer. The following is an example.

Dynamic.create(payload: {"rating"=>['4']})
Dynamic.create(payload: {"rating"=>['3']})
Dynamic.where("payload -> 'rating' ? '4'").count

. json jsonb. , .

, jsonb Rails 4.2 .

+1

, JSONQuery Rails Gem: https://github.com/jcoglan/siren

0

Source: https://habr.com/ru/post/1530603/


All Articles