Passing additional parameters to Rails Generate Model?

后端 未结 4 2054
余生分开走
余生分开走 2021-02-08 01:38

I\'m new to Ruby and Rails and I\'m running Rails 3 on Ruby 1.9.2.

I want to create a model, so I looked at the documentation to get the table definition that I want, bu

相关标签:
4条回答
  • 2021-02-08 02:08

    You can pass "null".to_sym => false in your rails generate model.
    For example:

    rails g model client 'ClientName, "null".to_sym => false:string{100}'
    

    This will turn the "null" into a symbol, allowing it to run properly when running db:migrate

    0 讨论(0)
  • 2021-02-08 02:11

    As it turns out, the limit can (now) be specified in the command line:

    rails generate model user pseudo:string{30}
    

    Source: usage doc from Rails GitHub project

    Setting the default, however, still appears to require editing the migration manually.

    For additional migration options, see the official Rails migrations guide.

    0 讨论(0)
  • 2021-02-08 02:12

    Yes you do have to manually edit the migration file for that. The generator just offers a starting point, it does not do everything.

    Doing this in the migration file is very easy anyway.

    create_table :articles do |t|
      t.string :title, :null => false
      t.text   :details, :limit => 3000, :null => false
    end
    
    0 讨论(0)
  • 2021-02-08 02:15

    Try some tricks: belongs_to and index

    rails g model User username:string:index group:belongs_to
    

    This will create:

    class User < ActiveRecord::Base
      belongs_to :group
      attr_accessible :username
    end
    
    0 讨论(0)
提交回复
热议问题