Ruby on Rails生成模型field:type-field:type的选项是什么?

    技术2022-07-15  71

    本文翻译自:Ruby on Rails generates model field:type - what are the options for field:type?

    I'm trying to generate a new model and forget the syntax for referencing another model's ID. 我试图生成一个新模型,却忘记了引用另一个模型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. 我自己检查一下,但是在我所有的Ruby on Rails文档链接中,我还没有弄清楚如何找到确定的源。

    $ rails g model Item name:string description:text (and here either reference:product or references:product ). $ rails g model Item name:string description:text (此处为reference:product或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. 注意:我已经学到了很难的方法,如果我输错了这些选项之一并运行迁移,那么Ruby on Rails将会完全破坏我的数据库...而rake db:rollback则无能为力。 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... 我敢肯定我只是不了解一些东西,但是直到我明白了…… rails g model返回的“详细”信息仍然让我抓不住...


    #1楼

    参考:https://stackoom.com/question/IOYG/Ruby-on-Rails生成模型field-type-field-type的选项是什么


    #2楼

    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. 至少对我来说这是个问题。


    #3楼

    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. 最后,只需在<%= form-for @project后为我工作,然后添加url: { projects: :create } 。


    #4楼

    http://guides.rubyonrails.org should 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


    #5楼

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

    See the table definitions section. 请参阅表定义部分。


    #6楼

    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 model项目名称:字符串描述:文本产品:引用

    This code will add 'product_id' column in the Item table 此代码将在Item表中添加“ product_id”列

    Processed: 0.008, SQL: 9