Ruby on Rails 生成模型字段:类型 - 字段:类型有哪些选项?

322
我正在尝试生成一个新模型,但忘记了如何引用另一个模型的ID。我本可以自己查找,但在我所有的Ruby on Rails文档链接中,我还没找到权威的来源。代码是:$ rails g model Item name:string description:text(接下来是reference:productreferences:product)。但更好的问题是,我该在哪里或如何轻松地查找这种东西以备将来之需?请注意:我极其不希望因为选项打错字而运行我的迁移,因为这样Ruby on Rails会彻底破坏我的数据库...而rake db:rollback无法解决这种问题。我相信我只是理解有误,但在我明白之前,“rails g model”返回的“详细”信息仍然让我感到困惑。

那么:uniq和:index字段选项呢?就像在“rails g model title body:text tracking_id:integer:uniq”中一样。我找不到这些的文档。还有吗? - Kangur
14
生成模型的命令是 rails generate model --help - Dennis
1
使用版本控制将为您提供一种轻松的方式来回滚任何生成的文件。如果问题出现在数据库中...好吧,您总是可以执行db:schema:load。 - Hector Ordonez
8个回答

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

请参阅表定义部分。


14
你的回答中没有提到:reference或者:references,也没有解释如何将它们传递给你提供的链接中的生成器。嗯... - Meltemi
56
这完全没有回答问题。 - MikeEL
1
选择这个作为答案,但要知道 :references 也是一个选项。 - Meltemi
14
有没有一些确切定义这些列类型的文档?例如,stringtext 有什么区别?请注意,翻译后不得添加解释或其他内容。 - Grant Birchmeier
3
“uniq”和“index”后缀(以及所有类型)的用法都在“rails generate model”的说明文档中有详细介绍。您可以运行“rails g model”命令来查看使用文档。 - Dennis
显示剩余10条评论

192

要创建一个引用另一个模型的模型,请使用Ruby on Rails模型生成器:

$ rails g model wheel car:references

这将生成 app/models/wheel.rb 文件:
class Wheel < ActiveRecord::Base
  belongs_to :car
end

并添加以下迁移:

class CreateWheels < ActiveRecord::Migration
  def self.up
    create_table :wheels do |t|
      t.references :car

      t.timestamps
    end
  end

  def self.down
    drop_table :wheels
  end
end

当您运行迁移时,以下内容将出现在您的 db/schema.rb 文件中:
$ rake db:migrate

create_table "wheels", :force => true do |t|
  t.integer  "car_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

关于文档,Rails生成器的起点是Ruby on Rails: A Guide to The Rails Command Line,它会引导你前往API文档了解更多可用字段类型。


5
略微过时,但这是此问题的答案。+1 - omninonsense

7

$ rails g model Item name:string description:text product:references

这段代码用于生成一个名为“Item”的模型,其中包含名称(name)、描述(description)和产品(product)等属性。其中,“name”是字符串类型,“description”是文本类型,“product”是引用类型。

我也觉得这些指南难以使用。虽然易于理解,但很难找到我要找的内容。

此外,我有一些临时项目,在这些项目上运行rails generate命令。然后,一旦我让它们工作起来,就在我的真实项目上运行它。

以上代码参考:http://guides.rubyonrails.org/getting_started.html#associating-models


3

记得在输入该命令时不要将文本大写。 例如:

请这样书写:

rails g model product title:string description:text image_url:string price:decimal

请勿写:

rails g Model product title:string description:text image_url:string price:decimal

至少对我来说,这是个问题。


等等?!什么?我经常给我的模型名称大写字母开头!你看到了什么“问题”? - Meltemi
2
我不是在谈论你的模型名称,而是'Model'这个名称。 我尝试创建一个这样的模型: rails g Model product title:string 但是出现了:无法找到生成器Model。 所以我尝试这样做: rails g model product title:string 然后它就成功了。 - Victor Augusto
1
啊,以前没遇到过这个。好的提示! - Meltemi
2
问题不在于模型名称,而是“model”一词指代生成器。 “rails g model Product…” 没有问题。 - Asherah

3

0

我也遇到了同样的问题,但我的代码略有不同。

def new
 @project = Project.new
end

我的表单看起来像这样:

<%= form_for @project do |f| %>
     and so on....
<% end %>

那是完全正确的,所以我不知道该怎么解决它。

最终,只需添加

url: { projects: :create }

之后

<%= form-for @project ...%>

对我有用。


这可能是一个不错的答案,但我不确定它与原帖有什么关系,因为原帖是关于字段类型(:integer:string等)的。 - Meltemi

0
在ROR中创建一个引用其他模型的模型非常简单。
rails g model Item name:string description:text product:references
这段代码将在Item表中添加'product_id'列。

0

在创建模型时,您可以提及许多数据类型,例如:

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

语法:

field_type:data_type

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接