RubyMine 7.0.0 Web Help

In this section:

Generators for creating models

RubyMine makes it possible to stub out models using the Rails generators:

  • The model generator creates a model class that defines a database table, and a migration that defines columns within the table. Besides that, a unit test and a test fixture are created.
  • The scaffold generator creates a model class that defines a database table, and a migration that defines columns within the table. Besides that, a controller, forms and the other necessary resources are created.
To create a model
  1. Do one of the following:
    • Choose New on the context menu of an editor or Project tool window, or press Alt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertCtrl+N or Alt+InsertAlt+InsertAlt+InsertCtrl+N or Ctrl+EnterCommand N or Control EnterControl N or Control EnterCommand N or Control EnterControl N or Control Enter, and then choose New - Run Rails Generator.
    • Run Rails generator: choose Tools | Run Rails Generator, or press Ctrl+Alt+GCtrl+Alt+GCtrl+Alt+GCtrl+Alt+GCtrl+Alt+GCtrl+Alt+GCtrl+Alt+GCtrl+Alt+GAlt+GAlt Command GAlt GAlt Command GAlt G.
    • 4.5+With a Model Dependency diagram having the focus, choose New - Model on the context menu of the diagram background, or press Alt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertCtrl+N or Alt+InsertAlt+InsertAlt+InsertCtrl+N or Ctrl+EnterCommand N or Control EnterControl N or Control EnterCommand N or Control EnterControl N or Control Enter.
  2. In the dialog box that opens, start typing the generator name, for example, model or scaffold.

    rm_create_model

    Note that for creating models in diagram, only model generator is used.

  3. Type the name of a model to be created, in singular, and the list of fields and their types. Note that code completion is available for the field types.

    rm_create_model_fields

    A migration is created.

  4. Create columns in the table. To do that, you have to run the migration in one of the following ways:
    • Run migration as a script with the temporary run configuration. Click the migration hyperlink in the console to open the migration file in the editor:

      ruby_createModelConsole.png

      Then press Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+Shift+F10Ctrl+F9Shift+Alt+F6 or Shift+F6Ctrl+Shift+F10Shift+Meta+RControl Shift RControl Shift F10Control Shift RAlt Command F8. In this case the current migration runs in the development environment, or in the one defined in the environment.rb file.

    • Run migration as a Rake task. For example, you can press Ctrl+Alt+RCtrl+Alt+RCtrl+Alt+RCtrl+Alt+RCtrl+Alt+RCtrl+Alt+RCtrl+Alt+RCtrl+Alt+RAlt+RAlt RAlt RAlt RAlt R, and start typing mig. Then select db:migrate from the suggestion list.

      In the Execute db:migrate dialog box, select the desired migration, and the environment that defines the database to which the migration will be applied:

      ruby_runMigration.png

    Once a model is created, you can view all the fields declared in this model, in the Rails view, and navigate from these table column to the corresponding methods.

Note

A model can be created without fields. In this case, the corresponding table in the target database will contain no columns. You can add fields later using migrations.

See Also

Concepts:

Procedures:

Reference:

Web Resources: