简体   繁体   中英

Rails 4 - How to store an array on postgres

I need to save this array:

params[:products]

The array contains these information:

[{"'name'"=>"Product Name 1 ", "'id'"=>"2", "'quantity'"=>"2", "'accessory'"=>{"'id'"=>"8", "'name'"=>"Accessory Name 1"}}, {"'name'"=>"Product Name 2 ", "'id'"=>"5", "'quantity'"=>"1", "'accessory'"=>{"'id'"=>"40", "'name'"=>"Accessory Name 2"}}]

As you can see, accessory is another array.

The process is this: A front-end guy is givin me that array, So I want to store all data on order.rb model.

So, I have a couple of questions:

  • Do I need to have "array type field" on database?.
  • Which fields do I need?

I was looking for some examples and I've been trying this on my order model:

serialize :product

order = Order.new
order.product = [:product]
order.save
order.product

I red about this method too: http://api.rubyonrails.org/classes/ActiveRecord/Store.html

Maybe this is a basic question but I really don't know how to solve it. As you can see, I don't have code in any controller because I really don't know what I need to write.

Thank you for your help.

Besides hstore, another solution would be JSON, specifically I suggest you use the PostgreSQL type "jsonb" because it's more efficient, see the documentation:

There are two JSON data types: json and jsonb. They accept almost identical sets of values as input. The major practical difference is one of efficiency. The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage.

(more info here https://www.postgresql.org/docs/current/static/datatype-json.html )

So you have, similarly to hstore, a data structure that you can execute queries against, and this queries are quite fast as you can read above. This is a significant advantage over other strategies, eg serializing a Ruby hash and saving it directly in the DB.

Charles,

I suggest you to consider using hstore type of your postgres. There are few benefits of using it (performance, indexing of objects etc..).

enable_extension 'hstore'

This actually enables your PSQL have this support.

Your migration is going to be like this:

class AddRecommendationsToPages < ActiveRecord::Migration
  def change
    add_column :pages, :recommendations, :hstore
  end
end

And after that you can pass into your hstore anything you want..

irb(main):020:0> Page.last.recommendations
  Page Load (0.8ms)  SELECT  "pages".* FROM "pages"  ORDER BY "pages"."id" DESC LIMIT 1
=> {"products"=>"34,32"}
irb(main):021:0> Page
=> Page(id: integer, block: string, name: string, slug: string, title: string, h1: string, annotation: text, article: text, created_at: datetime, updated_at: datetime, position: integer, parent: integer, show: boolean, recommendations: hstore)
irb(main):022:0> Page.last.recommendations["products"]
  Page Load (0.6ms)  SELECT  "pages".* FROM "pages"  ORDER BY "pages"."id" DESC LIMIT 1
=> "34,32"
irb(main):023:0>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM