ruby-on-rails – 在rails上创建ruby的新表

我尝试在rails中创建一个新的表.我发现和尝试悲伤的每一个例子都不适合我
所以这就是我现在所尝试的:(我使用 Ruby版本1.9和Rails版本3.2.13
在终端中建立新的模式:
rails generate model content content_id:auto-generated,law_id:integer,parent_id:integer,titel:string,text:string,content:string,url:string

生成以下代码

class CreateContents < ActiveRecord::Migration
  def change
    create_table :contents do |t|
      t.auto-generated,:content_id
      t.integer,:law_id
      t.integer,:parent_id
      t.string,:titel
      t.string,:text
      t.string,:content
      t.string :url

      t.timestamps
    end
  end
end

如果我尝试耙db:migrate我收到以下错误消息:

Syntax error,unexpected ',',expecting keyword_end
      t.auto-generated,:content_id
                       ^

如果我删除“,”我得到这个错误信息:

Syntax error,unexpected tSYMBEG,expecting keyword_do or '{' or '('
      t.auto-generated :content_id
                        ^

我的研究让我也以这种方式创建一个表:

class CreateContents < ActiveRecord::Migration
  def change
    create_table :contents do |t|
      t.auto-generated "content_id"
      t.integer "law_id"
      t.integer "parent_id"
      t.string "titel"
      t.string "text"
      t.string "content"
      t.string "url"

      t.timestamps
    end
  end
end

如果我试图用这个例子耙数据分析器,我得到这个错误信息:

Syntax error,unexpected tSTRING_BEG,expecting keyword_do or '{' or '('
      t.auto-generated "content_id"
                        ^

我做错了什么

解决方法

自动生成不是支持的列类型.

Active Record支持以下数据库列类型:

:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:primary_key
:string
:text
:time
:timestamp

http://guides.rubyonrails.org/migrations.html#supported-types的更多信息

Rails将自动为您创建列ID,因此只需将迁移编辑为以下内容即可

class CreateContents < ActiveRecord::Migration
  def change
    create_table :contents do |t|
      t.integer "law_id"
      t.integer "parent_id"
      t.string "titel"
      t.string "text"
      t.string "content"
      t.string "url"

      t.timestamps
    end
  end
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...