Advanced rails model generators

Posted by : on

Category : Rails

There are some people who give advice to not use rails generators and create models, controllers and etc. things manually. I don’t agree with them and my advice here is to figure out deeply how they work and then make conclusion.

In this post I will describe the most often and useful generator - it’s a model generator. I bet if you don’t use rails generators yet this post will make you to change your work. Using Rails generators saves your time, increases performance, helps to get consistent data for your application.

Basic usage

  • Let’s start with simple example: rails g model user email

  • This command will generate user model with email field type of string, migration which creates users table, test for model and factory (if you have it). You are able to generate model with few fields like this: rails g model user first_name last_name email

  • This example will generate yet model with 3 string fields: first_name, last_name and email.

  • If you want to have model with different type of string pass type after field name following by : and type. Example: rails g model user email age:integer

  • You can pass –option parameter to generator. It will inherit generating class from passed name to achieve STI (sing table inheritance): rails g model consumer --parent user

This example generates model:

class Consumer < User
end

Advanced usage

  • Sometime we need to automatically add some indexes on columns in migration. Then it can be doen like: rails g model user email:index location_id:integer:index

  • Or uniq index: rails g model user pseudo:string:uniq

  • Set limit for field of integer, string, text and binary fields: rails generate model user pseudo:string{30}

  • Special syntax to generate decimal field with scale and precision: rails generate model product 'price:decimal{10,2}'

  • You can combine any single curly brace option with the index options: rails generate model user username:string{30}:uniq

And the last useful feature of generators - it’s options to generate reference columns (fields which are used in rails as foreign keys): rails generate model photo album:references

This command will generate photos table with integer field album_id and also it will add index for this field automatically. Make sure in it by looking at generated migration:

class CreatePhotos < ActiveRecord::Migration
  def change
    create_table :photos do |t|
      t.references :album

      t.timestamps
    end
    add_index :photos, :album_id
  end
end
  • For polymorphic reference use this syntax: rails generate model product supplier:references{polymorphic}

  • Polymorphic reference with indexes: rails generate model product supplier:references{polymorphic}:index

Enjoy !!!



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in