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

时间:2022-05-05 20:23:57

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的语法。

$ 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?

$ rails g模型项目名称:字符串描述:文本(这里要么引用:产品,要么引用:产品)。但更好的问题是,我可以在哪里或如何在未来轻易地找到这种愚蠢的东西呢?

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...

注意:我学到了一种很困难的方法:如果我错误地编写了其中一个选项并运行了迁移,那么Ruby on Rails将会完全破坏我的数据库……和rake db:回滚对这样的错误是无效的。我确信我只是不明白一些事情,但在我明白之前……rails g模型返回的“详细”信息仍然让我挠头……

6 个解决方案

#1


443  

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

See the table definitions section.

参见表定义部分。

#2


183  

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:

生产应用程序/模型/ 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中结束。

$ 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.

至于文档,rails生成器的起点是Ruby on rails: rails命令行指南,它向您提供更多关于可用字段类型的API文档。

#3


7  

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

$ rails g模型项目名称:字符串描述:文本产品:引用

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.

此外,我还有运行rails生成命令的临时项目。一旦我让他们工作,我就在我的真实项目上运行。

Reference for the above code: http://guides.rubyonrails.org/getting_started.html#associating-models

上面代码的引用:http://guides.rubyonrails.org/getting_star.html #关联模型

#4


3  

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.

至少对我来说是个问题。

#5


3  

http://guides.rubyonrails.org should be a good site if you're trying to get through the basic stuff in Ruby on Rails.

http://guides.rubyonrails.org应该是一个很好的站点,如果你想了解Ruby on Rails的基本内容的话。

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_start.html #关联模型

#6


0  

I had the same issue, but my code was a little bit different.

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

def new @project = Project.new end

def new @project = Project。新结束

And my form looked like this:

我的表格是这样的:

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

< % = f form_for @project做| | % >等等....< % % >结束

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.

最后,添加url: {projects: create}后<%= form-for @project为我工作。

#1


443  

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

See the table definitions section.

参见表定义部分。

#2


183  

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:

生产应用程序/模型/ 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中结束。

$ 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.

至于文档,rails生成器的起点是Ruby on rails: rails命令行指南,它向您提供更多关于可用字段类型的API文档。

#3


7  

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

$ rails g模型项目名称:字符串描述:文本产品:引用

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.

此外,我还有运行rails生成命令的临时项目。一旦我让他们工作,我就在我的真实项目上运行。

Reference for the above code: http://guides.rubyonrails.org/getting_started.html#associating-models

上面代码的引用:http://guides.rubyonrails.org/getting_star.html #关联模型

#4


3  

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.

至少对我来说是个问题。

#5


3  

http://guides.rubyonrails.org should be a good site if you're trying to get through the basic stuff in Ruby on Rails.

http://guides.rubyonrails.org应该是一个很好的站点,如果你想了解Ruby on Rails的基本内容的话。

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_start.html #关联模型

#6


0  

I had the same issue, but my code was a little bit different.

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

def new @project = Project.new end

def new @project = Project。新结束

And my form looked like this:

我的表格是这样的:

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

< % = f form_for @project做| | % >等等....< % % >结束

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.

最后,添加url: {projects: create}后<%= form-for @project为我工作。