【问题标题】:Rails migration of an array inside a model doesn't work模型内数组的 Rails 迁移不起作用
【发布时间】:2016-07-16 22:52:36
【问题描述】:

我正在尝试创建一个模型,其中包含一些数组。通过此命令创建迁移文件后:

rails g model post poster_first_name:string poster_first_name:string email:string poster_id:integer poster_profile_pic:string message:string post_type:string comments:text

迁移文件已创建,我将迁移文件修改为如下所示(尝试创建 post_typecomments 数组):

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.integer :poster_id,        null: false, default: 0
      t.string :poster_first_name,              default: ""
      t.string :poster_last_name,               default: ""
      t.string :poster_profile_pic,             default: ""
      t.string :message,           null: false, default: ""
      t.string :post_type,         array: true, default: []
      t.text :comments,            array: true, default: []

      t.timestamps null: false
    end
  end
end

此迁移文件看起来正确。在我跑完rake db:migrate

这是我的架构的外观:

 create_table "posts", force: :cascade do |t|
   t.integer  "poster_id",          default: 0,          null: false
   t.string   "poster_first_name",  default: ""
   t.string   "poster_last_name",   default: ""
   t.string   "poster_profile_pic", default: ""
   t.string   "message",            default: "",         null: false
   t.string   "post_type",          default: "--- []\n"
   t.text     "comments",           default: "--- []\n"
   t.datetime "created_at",                              null: false
   t.datetime "updated_at",                              null: false
 end

所以基本上 rails 不明白这些字段是数组。我尝试了很多不同的东西。但没有运气。我将不胜感激任何帮助。 (这可能是一个菜鸟问题)

【问题讨论】:

  • 您使用的 Rails 版本是什么?
  • 我的 Rails 版本:Rails 4.2.6

标签: ruby-on-rails arrays ruby rails-migrations


【解决方案1】:

请查看此答案:Ruby on Rails: Submitting an array in a form

这样你就可以了解如何使用它了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 2012-09-08
    • 2016-06-25
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多