为什么我不能这样做:
class CreateModels < ActiveRecord::Migration
def self.up
create_table :fruit do |t|
t.array :apples
end
end
end
还有其他方法可以使数组(“苹果”)成为 Fruit 类实例的属性吗?
为什么我不能这样做:
class CreateModels < ActiveRecord::Migration
def self.up
create_table :fruit do |t|
t.array :apples
end
end
end
还有其他方法可以使数组(“苹果”)成为 Fruit 类实例的属性吗?
在 Rails 4 和使用 PostgreSQL 中,您实际上可以在数据库中使用数组类型:
移民:
class CreateSomething < ActiveRecord::Migration
def change
create_table :something do |t|
t.string :some_array, array: true, default: []
t.timestamps
end
end
end
查看关于关联的 Rails 指南(特别注意 has_many)。
您可以使用数据库支持的任何列类型(使用t.column
而不是t.type
),尽管如果跨数据库的可移植性是一个问题,我相信建议坚持使用 activerecord 明确支持的类型。
水果有很多个苹果似乎有点有趣,但也许这只是一个例子?(我希望苹果是水果的一个子类)。