Ruby on Rails 生成模型 field:type - field:type 有哪些选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4384284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails generates model field:type - what are the options for field:type?
提问by Meltemi
I'm trying to generate a new model and forget the syntax for referencing another model's ID. I'd look it up myself, but I haven't figured out, among all my Ruby on Rails documentation links, how to find the definitive source.
我正在尝试生成一个新模型,但忘记了引用另一个模型 ID 的语法。我会自己查找,但我还没有弄清楚,在我所有的 Ruby on Rails 文档链接中,如何找到最终的来源。
$ rails g model Item name:string description:text(and here either reference:productor references:product). But the better question is whereor howcan I look for this kind of silliness easily in the future?
$ rails g model Item name:string description:text(这里无论是reference:product或references:product)。但更好的问题是,我将来在哪里或如何轻松地寻找这种愚蠢?
Note: I've learned the hard way that if I mistype one of these options and run my migrationthen Ruby on Rails will totally screw up my database... and rake db:rollbackis powerless against such screwups. I'm sure I'm just not understanding something, but until I do... the "detailed" information returned by rails g modelstill leaves me scratching...
注意:我已经学会了一种艰难的方式,如果我错误地输入了这些选项之一并运行我的迁移,那么 Ruby on Rails 将完全搞砸我的数据库......并且rake db:rollback对这种搞砸无能为力。我确定我只是不理解某些东西,但是在我理解之前......返回的“详细”信息rails g model仍然让我抓挠......
回答by Paul Schreiber
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp,
:time, :date, :binary, :boolean, :references
See the table definitionssection.
请参阅表定义部分。
回答by Troy
To create a model that references another, use the Ruby on Rails model generator:
要创建引用另一个模型的模型,请使用 Ruby on Rails 模型生成器:
$ rails g model wheel car:references
That produces app/models/wheel.rb:
这会产生app/models/wheel.rb:
class Wheel < ActiveRecord::Base
belongs_to :car
end
And adds the following migration:
并添加以下迁移:
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
When you run the migration, the following will end up in your db/schema.rb:
当您运行迁移时,以下内容将最终出现在您的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
As for documentation, a starting point for rails generators is Ruby on Rails: A Guide to The Rails Command Linewhich points you to API Documentationfor more about available field types.
至于文档,rails 生成器的起点是Ruby on Rails:Rails 命令行指南,它指向API 文档以获取有关可用字段类型的更多信息。
回答by B Seven
$ rails g model Item name:string description:text product:references
$ rails g model Item name:string description:text product:references
I too found the guides difficult to use. Easy to understand, but hard to find what I am looking for.
我也发现这些指南很难使用。容易理解,但很难找到我要找的东西。
Also, I have temp projects that I run the rails generatecommands on. Then once I get them working I run it on my real project.
另外,我有运行rails generate命令的临时项目。然后一旦我让他们工作,我就会在我的真实项目中运行它。
Reference for the above code: http://guides.rubyonrails.org/getting_started.html#associating-models
以上代码参考:http: //guides.rubyonrails.org/getting_started.html#associating-models
回答by Raghu
http://guides.rubyonrails.orgshould be a good site if you're trying to get through the basic stuff in Ruby on Rails.
如果您想了解 Ruby on Rails 的基本内容,http://guides.rubyonrails.org应该是一个不错的站点。
Here is a link to associate models while you generate them: http://guides.rubyonrails.org/getting_started.html#associating-models
这是在生成模型时关联模型的链接:http: //guides.rubyonrails.org/getting_started.html#associating-models
回答by Victor Augusto
Remember to not capitalize your text when writing this command. For example:
请记住,在编写此命令时不要将文本大写。例如:
Do write:
写:
rails g model product title:string description:text image_url:string price:decimal
Do not write:
不要写:
rails g Model product title:string description:text image_url:string price:decimal
At least it was a problem to me.
至少对我来说是个问题。
回答by justinedps26
I had the same issue, but my code was a little bit different.
我有同样的问题,但我的代码有点不同。
def new
@project = Project.new
end
And my form looked like this:
我的表格是这样的:
<%= form_for @project do |f| %>
and so on....
<% end %>
That was totally correct, so I didn't know how to figure it out.
这是完全正确的,所以我不知道如何弄清楚。
Finally, just adding
最后,只需添加
url: { projects: :create }
after
后
<%= form-for @project ...%>
worked for me.
对我来说有效。
回答by chandanjha
It's very simple in ROR to create a model that references other.
在 ROR 中创建引用其他模型的模型非常简单。
rails g model Item name:string description:text product:references
rails g 模型项目名称:字符串描述:文本产品:参考
This code will add 'product_id' column in the Item table
此代码将在 Item 表中添加“product_id”列
回答by chandanjha
There are lots of data types you can mention while creating model, some examples are:
在创建模型时,您可以提及很多数据类型,一些示例是:
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references
syntax:
句法:
field_type:data_type

