在rails上的ruby中创建新表 [英] Create new Table in ruby on rails

查看:147
本文介绍了在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

如果我尝试rake db:migrate我得到以下错误信息:

 语法错误,意外的',',expecting keyword_end 
t.auto生成,:content_id
^

如果我删除,我收到此错误消息:

 语法错误,意外的tSYMBEG,期望关键字_或者{'或'('
t.auto-generated:content_id
^

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

  class CreateContents < ActiveRecord :: Migration 
def change
create_table:contents do | t |
t.auto-generatedcontent_id
t.integerlaw_id
t.integerparent_id
t.stringtitel
t.string text
t.stringcontent
t.stringurl

t.timestamps
end
end
end

如果我尝试使用该示例获取db,则会收到此错误消息:

 语法错误,意外的tSTRING_BEG,expecting keyword_do或'{'或'('
t.auto-generatedcontent_id
^

我做错了什么?

解决方案

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



支持以下数据库列类型:

 :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.integerlaw_id
t.integerparent_id
t.stringtitel
t.stringtext
t.stringcontent
t.stringurl

t.timestamps
end
end
end


I try to create a new table in rails. Every example I find and try sadly does not work with me... so that's what I tried till now: (I use Ruby version 1.9 and Rails Version 3.2.13 making a new model in the terminal:

rails generate model content content_id:auto-generated, law_id:integer, parent_id:integer, titel:string, text:string, content:string, url:string

that generated following code:

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

if I try to rake db:migrate i get the following error message:

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

if I remove the "," I get this error message:

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

my research got me to also to this way of creating a table:

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

if I try to rake the db with that example I get this error message:

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

What do I do wrong?

解决方案

auto-generated is not a supported column type.

Active Record supports the following database column types:

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

More info in http://guides.rubyonrails.org/migrations.html#supported-types

Rails will create the column id automatically for you, thus just edit your migration to the following

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

这篇关于在rails上的ruby中创建新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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