简体   繁体   English

Rails 4-如何在postgres上存储数组

[英]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. 如您所见, accessory是另一个阵列。

The process is this: A front-end guy is givin me that array, So I want to store all data on order.rb model. 过程是这样的:一个前端人员为我提供了该数组,因此我想将所有数据存储在order.rb模型中。

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: 我正在寻找一些示例,并且已经在我的order模型上进行了尝试:

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 我对这种方法也很满意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: 除了hstore之外,另一个解决方案是JSON,特别是我建议您使用PostgreSQL类型的“ jsonb”,因为它效率更高,请参阅文档:

There are two JSON data types: json and jsonb. 有两种JSON数据类型:json和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; json数据类型存储输入文本的精确副本,处理函数必须在每次执行时重新解析; 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数据以分解后的二进制格式存储,由于增加了转换开销,因此输入速度稍慢,但由于不需要解析,因此处理速度明显更快。 jsonb also supports indexing, which can be a significant advantage. jsonb还支持索引编制,这可能是一个很大的优势。

(more info here https://www.postgresql.org/docs/current/static/datatype-json.html ) (更多信息在这里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. 因此,与hstore相似,您可以执行一个查询所依据的数据结构,并且该查询非常快,如您在上面所读。 This is a significant advantage over other strategies, eg serializing a Ruby hash and saving it directly in the DB. 与其他策略相比,这是一个显着的优势,例如序列化Ruby哈希并将其直接保存在数据库中。

Charles, 查尔斯

I suggest you to consider using hstore type of your postgres. 我建议您考虑使用Postgres的hstore类型。 There are few benefits of using it (performance, indexing of objects etc..). 使用它几乎没有好处(性能,对象索引等)。

enable_extension 'hstore'

This actually enables your PSQL have this support. 这实际上使您的PSQL具有此支持。

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.. 之后,您可以将所需的任何内容传递到hstore。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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