Sebastian Zeki
Sebastian Zeki

Reputation: 6874

How to convert model to migration

I am a bit confused by this. On creating a model, I always thought that this would produce a migration which then gets pushed into a database. So when I type

rails g model Userss name:string email:string group:integer

in the model I get:

class Userss < ActiveRecord::Base
end

and in the migration (20150619151857_create_usersses.rb)I get:

class CreateUsersses < ActiveRecord::Migration
  def change
    create_table :usersses do |t|
      t.string :name
      t.string :email
      t.integer :group

      t.timestamps
    end
  end
end

however, if I type rails g model Userss name:string email:string group:integer --migration=false

I get the same model, but then when I run a migration as rails g migration userss, I get a file which is not called ....create_userss.rb, but is called 20150619152316_userss.rb and it contains:

class Userss < ActiveRecord::Migration
  def change
  end
end

so why is there a difference in the migration file?

Upvotes: 0

Views: 89

Answers (2)

max
max

Reputation: 102046

I think you confused about how the rails migration generators work. They actually don't care at all about your models.

When you run:

rails g model user name:string

Rails just runs the migration generator for you:

rails g migration CreateUsers name:string

Rails can figure out from the naming of the migration what kind of migration to generate. CreateUsers name:string will obviously create a create table migration.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
    end
  end
end

AddFooToUsers foo:int will create a migration to alter users:

class AddFooToUsers < ActiveRecord::Migration
  def change
    add_column :users, :foo, :int
  end
end

Rails does not really care if you use camelcase or snakecase for the migration name in the generator add_fields_to_users or AddFieldsToUsers give the same result.

Also the Rails convention is to use the singular form for model names (User, Fruit, Car etc) this is very important since it lets rails automatically figure out the connection between controllers and models.

Even more important is that you don't misspell common english words like users since it will confuse every poor sod who has to maintain your code.

Upvotes: 2

David Routen
David Routen

Reputation: 349

If you want to create a migration that will add columns to the Userss table, do this:

rails g migration add_fields_to_userss name:string email:string group:integer

That will create a migration to add columns to an existing table. If you don't add the _to_userss at the end then it will just create a generic migration file for you.

rails g model Userss name:string ... will create a migration that, when run, will create the table Userss with the specified columns.

rails g migration blah_blah_blah_to_userss name:string ... will create a migration that only adds columns and references to the existing table Userss.

Upvotes: 2

Related Questions