Why can't I create an array as a column in a table in Rails?

前端 未结 3 677
囚心锁ツ
囚心锁ツ 2021-01-05 19:23

Why can\'t I do something like this:

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


        
3条回答
  •  灰色年华
    2021-01-05 19:58

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

    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.

    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).

提交回复
热议问题