ruby on rails - How to convert model to migration -
i bit confused this. on creating model, thought produce migration gets pushed database. when type
rails g model userss name:string email:string group:integer
in model get:
class userss < activerecord::base end
and in migration (20150619151857_create_usersses.rb
)i get:
class createusersses < activerecord::migration def change create_table :usersses |t| t.string :name t.string :email t.integer :group t.timestamps end end end
however, if type rails g model userss name:string email:string group:integer --migration=false
i same model, when run migration rails g migration userss
, file not called ....create_userss.rb
, called 20150619152316_userss.rb
, contains:
class userss < activerecord::migration def change end end
so why there difference in migration file?
i think confused how rails migration generators work. don't care @ models.
when run:
rails g model user name:string
rails runs migration generator you:
rails g migration createusers name:string
rails can figure out naming of migration kind of migration generate. createusers name:string
create create table migration.
class createusers < activerecord::migration def change create_table :users |t| t.string :name end end end
addfootousers foo:int
create migration alter users
:
class addfootousers < activerecord::migration def change add_column :users, :foo, :int end end
rails not care if use camelcase or snakecase migration name in generator add_fields_to_users
or addfieldstousers
give same result.
also rails convention use singular form model names (user, fruit, car etc) important since lets rails automatically figure out connection between controllers , models.
even more important don't misspell common english words users since confuse every poor sod has maintain code.
Comments
Post a Comment