Ruby on Rails 生成模型 field:type - field:type 有哪些选项?
我正在尝试生成一个新模型,但忘记了引用另一个模型 ID 的语法。我会自己查找,但在我的所有 Ruby on Rails 文档链接中,我还没有弄清楚如何找到最终的来源。
$rails g model 项目名称:字符串描述:文本
(此处为reference:product
或references:product
)。但更好的问题是我将来可以在哪里或如何轻松地寻找这种愚蠢的行为?
注意:我经历了惨痛的教训,如果我错误输入其中一个选项并运行我的迁移,那么 Ruby on Rails 将完全搞砸我的数据库......并且 rake db:rollback< /code> 对于此类错误无能为力。我确信我只是不理解某些东西,但在我理解之前......由
rails g model
返回的“详细”信息仍然让我摸不着头脑......
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.
$ rails g model Item name:string description:text
(and here either reference:product
or references:product
). But the better question is where or how can I look for this kind of silliness easily in the future?
Note: I've learned the hard way that if I mistype one of these options and run my migration then Ruby on Rails will totally screw up my database... and rake db:rollback
is powerless against such screwups. I'm sure I'm just not understanding something, but until I do... the "detailed" information returned by rails g model
still leaves me scratching...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请参阅表定义部分。
See the table definitions section.
要创建引用另一个模型的模型,请使用 Ruby on Rails 模型生成器:
生成 app/models/wheel.rb:
并添加以下迁移:
运行迁移时,最终将出现以下结果在您的 db/schema.rb 中:
至于文档,rails 生成器的起点是 Ruby on Rails:Rails 命令行指南,它会将您指向 API 文档 了解有关可用字段类型的更多信息。
To create a model that references another, use the Ruby on Rails model generator:
That produces app/models/wheel.rb:
And adds the following migration:
When you run the migration, the following will end up in your db/schema.rb:
As for documentation, a starting point for rails generators is Ruby on Rails: A Guide to The Rails Command Line which points you to API Documentation for more about available field types.
$rails g model 项目名称:字符串描述:文本产品:参考
我也发现这些指南很难使用。很容易理解,但很难找到我想要的东西。
另外,我还有运行
rails generated
命令的临时项目。然后,一旦我让它们工作,我就在我的真实项目上运行它。上述代码的参考: http://guides.rubyonrails.org/getting_started.html#associating -模型
$ 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 generate
commands on. Then once I get them working I run it on my real project.Reference for the above code: http://guides.rubyonrails.org/getting_started.html#associating-models
请记住在编写此命令时不要将文本大写。
例如:
一定要写:
不要写:
至少这对我来说是一个问题。
Remember to not capitalize your text when writing this command.
For example:
Do write:
Do not write:
At least it was a problem to me.
如果您想了解基本知识,http://guides.rubyonrails.org 应该是一个不错的网站Ruby on Rails 中的东西。
以下是在生成模型时关联模型的链接:
http://guides.rubyonrails.org/getting_started.html#associating-models
http://guides.rubyonrails.org should be a good site if you're trying to get through the basic stuff in Ruby on Rails.
Here is a link to associate models while you generate them:
http://guides.rubyonrails.org/getting_started.html#associating-models
在 ROR 中创建引用其他模型的模型非常简单。
Rails g model 项目名称:字符串描述:文本产品:引用
此代码将在项目表中添加“product_id”列
It's very simple in ROR to create a model that references other.
rails g model Item name:string description:text product:references
This code will add 'product_id' column in the Item table
创建模型时可以提到很多数据类型,一些示例是:
语法:
There are lots of data types you can mention while creating model, some examples are:
syntax:
我有同样的问题,但我的代码有点不同。
我的表格看起来像这样:
这是完全正确的,所以我不知道如何弄清楚。
添加。
后
最后,只是在为我工作
I had the same issue, but my code was a little bit different.
And my form looked like this:
That was totally correct, so I didn't know how to figure it out.
Finally, just adding
after
worked for me.