为什么我不能创建一个数组在Rails中一个表中的列? [英] Why can't I create an array as a column in a table in Rails?

查看:141
本文介绍了为什么我不能创建一个数组在Rails中一个表中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能做这样的事情:

Why can't I do something like this:

class CreateModels < ActiveRecord::Migration
  def self.up
    create_table :fruit do |t|
      t.array :apples
    end
  end
end

有一些其他的方法,使一个数组(苹果)是水果类的实例的属性?

Is there some other way to make an array ("apples) be an attribute of an instance of the Fruit class?

推荐答案

查看的Rails指南协会(特别注意的has_many)。

Check out the Rails guide on associations (pay particular attention to has_many).

您可以使用您的数据库支持的任何字段类型(使用 t.column 而不是 t.type ) ,但如果整个数据块的便携性是一个问题,我相信它的建议,坚持通过ActiveRecord的明确支持的类型。

You can use any column type supported by your database (use t.column instead of t.type), although if portability across DBs is a concern, I believe it's recommended to stick to the types explicitly supported by activerecord.

这似乎有点儿滑稽的水果have_many苹果,但也许这只是一个例子吗? (我希望苹果是水果的一个子类)。

It seems kind of funny for fruit to have_many apples, but maybe that is just an example? (I would expect apples to be a subclass of fruit).

这篇关于为什么我不能创建一个数组在Rails中一个表中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆