Ruby on Rails 生成模型 field:type - field:type 有哪些选项?

发布于 2024-10-07 05:02:31 字数 482 浏览 8 评论 0原文

我正在尝试生成一个新模型,但忘记了引用另一个模型 ID 的语法。我会自己查找,但在我的所有 Ruby on Rails 文档链接中,我还没有弄清楚如何找到最终的来源。

$rails g model 项目名称:字符串描述:文本(此处为reference:productreferences: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

几味少女 2024-10-14 05:02:31
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp,
:time, :date, :binary, :boolean, :references

请参阅表定义部分。

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

See the table definitions section.

别在捏我脸啦 2024-10-14 05:02:31

要创建引用另一个模型的模型,请使用 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:Rails 命令行指南,它会将您指向 API 文档 了解有关可用字段类型的更多信息。

To create a model that references another, use the Ruby on Rails model generator:

$ rails g model wheel car:references

That produces 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:

$ 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 Line which points you to API Documentation for more about available field types.

被你宠の有点坏 2024-10-14 05:02:31

$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

独孤求败 2024-10-14 05:02:31

请记住在编写此命令时不要将文本大写。
例如:

一定要写:

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

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

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.

旧情别恋 2024-10-14 05:02:31

如果您想了解基本知识,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

十二 2024-10-14 05:02:31

在 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

暖阳 2024-10-14 05:02:31

创建模型时可以提到很多数据类型,一些示例是:

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

语法:

field_type:data_type

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
揽月 2024-10-14 05:02:31

我有同样的问题,但我的代码有点不同。

def new
 @project = Project.new
end

我的表格看起来像这样:

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

这是完全正确的,所以我不知道如何弄清楚。

添加。

url: { projects: :create }

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

最后,只是在为我工作

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文