I am new to Rails. I want to know the difference between these two methods of generating migrations:
rails g migration MigrationName
rails g model MigrationName
Is preferred for making alterations to the tables that already exists. What is the real difference between these 2?
Well the main difference is that the second one should be rails g model ModelName doesn't just create a migration, but also creates a model file and a spec file for that model. It also will most generate a create table migration, whereas with rails g migration MigrationName, you can just do very specific migrations such as adding an index, or adding/removing columns. Sections 2.1 and 2.2 will help you get a better grasp: http://guides.rubyonrails.org/migrations.html
rails g migration Filename parameters ...
This one generates a file in which you can write migration code. Like creating index or drop, ...
rails g model Tablename field field field:type ....
This on generates a file to generate a table with the give parameters.
See more when running rails g model
Related
New to Rails, so please feel free to edit my question should I get the terminology wrong!
I've created my MVC via scaffold, but have since made changes to the underlying table (renamed some columns, added some columns). Is there a way to regenerate the views that were created with the original scaffold such that they reflect the changes I've made?
If I run rails g scaffold Foo --migration=false --skip I get an error that my model name is already used in my application.
If delete the entire corresponding views folder (the one created by the original scaffold), and I run rails g scaffold Foo --migration=false --force the views generated still don't pick up all the fields I have in my model.
You could reverse the scaffold generation and regenerate a new scaffold with renamed and new columns.
If you already migrated the database you will need to roll it back.
rake db:rollback
Then you can reverse the scaffold generation.
rails destroy scaffold MyFoo
Then generate new scaffold with updated columns
rails generate scaffold MyFoo column_name_1:data_type column_name_2:data_type column_name_3:data_type
Otherwise you can manually rewrite your views and controllers to include the updated and new columns.
When running:
rails generate migration <someaction> field:type
I can see it is performing two actions:
Call invoke active_record
Create a migration file.
I understand perfectly why it is generating the migration file, but why does it invoke ActiveRecord? This bothers me because what if I want to create the migration file manually? how would I mimic this invocation (if it's even necessary..)?
The MigrationGenerator generator:
1) Loads your ORM (which by default in Rails is Active Record) to have it extend the correct 'ORM'::Migration class (again by default this is ActiveRecord::Migration)
2) It is itself an extension of the NamedBase generator which sees if your running Active Record to decide if it should pluralize the table names. This is so if you run
rails generate migration AddPartNumberToProducts part_number:string
or
rails generate migration AddPartNumberToProduct part_number:string
You get the same results in your file.
So the short answer is, you do not need to invoke active_record to create a migration by hand, but if you do and you are using Active Record make sure that your table names are pluralized in your migration file.
activerecord gem is invoked to generate the migration file.
If you look closely, the super class of a migration file is an ActiveRecord::Migration class.
I am new to Ruby on Rails . This might be a very foolish question.
I have created a migration using
rails generate migration Kapol name:string position:integer
rake db:migrate
Then using phpmyadmin i copied the database already present
I then opened up rails console
My question is can i use the method Kapol.find(1)??
because when i tried it using singular or plural it says
unitialized constant:Kapol
I know that there has to be a method but where to specify it?
You must generate a model in case to create a table for it, because the migration is usually used to modify existing tables.
It might be confusing that the model generator also creates migration file in your migrations folder. The only difference is that the model generator also generates initial code to create table, on the other hand, the migration generator creates only migration file without initial code.
rails generate model Kapol name:string position:integer
More information: http://guides.rubyonrails.org/getting_started.html#generating-a-model
If you're very new to Ruby on Rails, probably the best thing for you to do is create a scaffold, which gives you your migration file, your model file, your controller file, and various view files, test files, etc. etc., which all work well together. Then you can play with these and build up from there.
rails generate scaffold Kapol name:string position:integer
If you're happy with the migration that was automatically generated, then rake db:migrate and you're all set.
As Andrew says below, you can also just generate any of those files one at a time by replacing 'scaffold' with 'model', etc.
Your Kapol.find(1) is correct.
I wrote a simple migration file that creates around ten tables. It all created perfectly. Now I need to create ten equivalent model files in the app/models folder. I can do it manually. But I am wondering if there is any rake task available to do this.
Tips/advise on this is much appreciated.
Automatically: http://magicmodels.rubyforge.org/magic_model_generator/
Manually: http://forums.devshed.com/showpost.php?p=1957164&postcount=2
You can create a model and migration using the same rails model generator. This will also create a unit test and fixtures.
Rails 2.3.x:
script/generate model Person name:string
Rails 3.0.x
rails g model Person name:string
You can also use the following options (taken from Rails 2.3.8 and might have changed in 3.0)
Options:
--skip-timestamps Don't add timestamps to the migration file for this model
--skip-migration Don't generate a migration file for this model
--skip-fixture Don't generation a fixture file for this model
I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --skip-migration
Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?
Have you generated a schema file from your existing database? If you run the command
rake db:schema:dump
and then re-generate your scaffold this should fix the problem.
Additionally you may wish to check out Dr Nic's Magic Model generator. This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.
UPDATE
I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.
You have two choices here either
Pass in the column name pairs as well as skip-migration or
Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration