Ruby模型的数组属性

Ruby模型的数组属性,ruby,database-design,attributes,migration,ruby-on-rails-3.1,Ruby,Database Design,Attributes,Migration,Ruby On Rails 3.1,是否可以为作为数组的类创建属性?我试着读书,但没能从中得到什么。我想这样做: class CreateArches < ActiveRecord::Migration def change create_table :arches do |t| t.string :name t.array :thearray t.timestamps end end end my_model.thearray = [1,2,3] 迁移: t.

是否可以为作为数组的类创建属性?我试着读书,但没能从中得到什么。我想这样做:

class CreateArches < ActiveRecord::Migration
  def change
    create_table :arches do |t|
      t.string :name
      t.array :thearray
      t.timestamps
    end
  end
end
my_model.thearray = [1,2,3]
迁移:

t.text :thearray, :default => [].to_yaml
add_column :model, :attribute, :text, array: true, default: []
在模型使用中:

正如Marnen在他的回答中所说,最好知道您希望在该数组中存储什么类型的信息,序列化属性可能不是最好的选择

[Marten Veldthuis的警告]更改序列化数组时要小心。如果您直接这样更改它:

class CreateArches < ActiveRecord::Migration
  def change
    create_table :arches do |t|
      t.string :name
      t.array :thearray
      t.timestamps
    end
  end
end
my_model.thearray = [1,2,3]
这很好,但如果您这样做:

my_model.thearray << 4

my_model.thearray创建一个带有文本字段的模型

> rails g model Arches thearray:text
  invoke  active_record
  create    db/migrate/20111111174052_create_arches.rb
  create    app/models/arches.rb
  invoke    test_unit
  create      test/unit/arches_test.rb
  create      test/fixtures/arches.yml
> rake db:migrate
==  CreateArches: migrating ===================================================
-- create_table(:arches)
   -> 0.0012s
==  CreateArches: migrated (0.0013s) ==========================================
编辑模型,使字段序列化为数组

class Arches < ActiveRecord::Base
  serialize :thearray,Array
end
class
试一试

ruby-1.8.7-p299 :001 > a = Arches.new
 => #<Arches id: nil, thearray: [], created_at: nil, updated_at: nil> 
ruby-1.8.7-p299 :002 > a.thearray
 => [] 
ruby-1.8.7-p299 :003 > a.thearray << "test"
 => ["test"] 
ruby-1.8.7-p299:001>a=arch.new
=> # 
ruby-1.8.7-p299:002>a.thearray
=> [] 
ruby-1.8.7-p299:003>a.thearray[“测试”]

虽然您可以按照tokland的建议使用序列化数组,但在关系数据库中这很少是一个好主意。您有三个更好的选择:

  • 如果数组包含实体对象,则最好将其建模为具有许多关系的
  • 如果数组实际上只是一个值数组,比如数字,那么您可能希望将每个值放在一个单独的字段中,并使用由
组成的
u
  • 如果要使用大量的数组值,而这些数组值并不是
    所具有的
    s,那么您可能需要研究一个实际支持数组字段的数据库。PostgreSQL可以做到这一点(Rails 4迁移中支持数组字段),但您可能希望使用非SQL数据库(如MongoDB)或对象持久性(如应该提供的)

  • 如果你能描述你的用例——也就是说,你在数组中得到了什么数据——我们可以尝试帮助你找出最佳的行动方案。

    如果使用Postgres,你可以使用它的:

    迁移:

    t.text :thearray, :default => [].to_yaml
    
    add_column :model, :attribute, :text, array: true, default: []
    
    然后像数组一样使用它:

    model.attribute # []
    model.attribute = ["d"] #["d"]
    model.attribute << "e" # ["d", "e"]
    
    model.attribute#[]
    model.attribute=[“d”]#[“d”]
    
    model.attributeRails 6+

    在Rails 6中(在较小程度上是Rails 5),您可以使用,这将允许您创建一个类型化的“virtual”/“non-db-backed”列,甚至是一个默认属性。例如:

    attribute :categories, :jsonb, array: true, default: [{ foo: 'bar' }, { fizz: 'buzz' }]
    
    其结果是:

    Example.new
    
    
    #<Example:0x00007fccda7920f8> {
                "id" => nil,
        "created_at" => nil,
        "updated_at" => nil,
        "categories" => [
                          [0] {
                            "foo" => "bar"
                          },
                          [1] {
                            "fizz" => "buzz"
                          }
                        ]
    }
    
    Example.new
    # {
    “id”=>nil,
    “在”=>零处创建,
    “更新位置”=>nil,
    “类别”=>[
    [0] {
    “foo”=>“bar”
    },
    [1] {
    “嘶嘶声”=>“嗡嗡声”
    }
    ]
    }
    

    请注意,您可以使用任何类型,但如果它还不可用,则必须注册它。在上面的例子中,我使用
    PostgeSQL
    作为数据库,它已经注册了
    :jsonb
    作为一个类型。

    Ah好的,我的数组将只包含字符串。你认为呢?数组代表什么?首先,为什么要使用它?我最初写道,我不知道有任何SQL数据库以本机方式存储数组或散列。事实证明,Postgres在本地处理这两种类型,Rails 4在迁移和ActiveRecord中支持这两种类型。使用Postgres还有一个理由:+@Dorian的1取决于用例。这里没有足够的信息可以知道。它将是一个字符串数组。序列化是一条路吗?@tquarton几乎肯定不是。看看我的答案。