微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/ruby/271699.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐