26
loading...
This website collects cookies to deliver better user experience
<rails generate generator-name details...>
. This can also be shortened with the alias for generate
, g
, to <rails g generator-name details....>
. In these examples, I'll use the alias g
, but they can be written either way. For this example, we'll imagine that we are building an application with Book, Author, and Publisher models. <rails g model Modelname>
rails g model Book
<rails g controller Pluralname [action action] [options]>
rails g controller Books new create show update
<rails g resource Modelname column_name:data_type>
rails g resource Author name:string
<rails g scaffold Modelname column_name:data_type>
rails g scaffold Publisher name:string
belongs_to
relationships via the command line. Rails will also work behind the scenes during database lookups to enforce requirements of these relationships when they are generated in this way. Using our examples above, we can imagine that a book belongs to an author. If we re-create our Book, we can add this relationship in:rails g resource Book name:string author:references
<rails g task task_name>
rails g task track_books
tasks
folder (lib/tasks/track_books.rake
) where you can define tasks that you need in your app.destroy
, but it can be aliased as d
.<rails d model Name>
rails d model Book
rails g model Book
, destroying the associated test files, the Model, and the boilerplate migration.