Why does "rails generate migration" call "invoke activerecord" - ruby-on-rails

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.

Related

Difference between generating migrations through these 2 methods

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

Delete model and migration file rails 4

I was attempting to add a tagging model to my rails blog. But I accidentally generated a "Tags.rb" model as opposed to "Tag.rb", after reading the guide I realized that making "tag" plural was a mistake when it comes to model. I rolled back the migration using
rake db:rollback
and then
rails destroy model Tags.rb
and this was what i got back
invoke active_record
remove /home/migration/templates/create_table_migration.rb
remove app/models/tags.rb.rb
invoke test_unit
remove test/models/tags.rb_test.rb
remove test/fixtures/tags.rbs.yml
When I come back to the model folder though its still there.
Please help :)
Just give
rails destroy model Tag
Try using tags plural without the additional .rb, like
rails destroy model Tags
Hope this helps
You can manually delete the files, or just manually remove the "s"s.
try this
bundle exec rake db:rollback
and then delete your model as
rails destroy model <model_name>
now totaly deleted.
You can delete the migration files in migrate folder and model in your model folder and then you can again create new one which you wants.
you have to run that commands
rails g model tag
Then migrate database
rake db:migrate
First off, when generating a Rails model using rails generate model, don't add .rb to the end of the model name. Rails automatically does that so if you add .rb yourself then you end up with a model named Tag.rb.rb, which is invalid.
You're running the remove generator with "Tags.rb". Did you generate the model originally using rails generate model Tags.rb? If you did, then running rails destroy model Tags.rb will hopefully remove all the files, but since the model name is invalid, it might not work as expected (your use case is outside the intended use case of the generator). If you originally generated the model using rails generate model Tags, then running rails destroy model Tags should work.
If you originally invoked rails generate model Tags.rb, and running the remove command isn't working (which again, in understandable given that Tags.rb is an invalid name--would have been nice if the generator had originally prohibited you from generating a Tags.rb.rb file, but you can't have everything):
If you can afford to destroy your database and rerun all the migrations, the easy solution will be to manually remove the added files, destroy the database, and rerun all migrations. Then you can add the correct model using rails generate model Tag.
If you can't afford to destroy your database, then you'll need to manually find the problem migration in the migration's folder, run the down command on that migration, and then manually delete that migration file and all the other files.

Access Migrations via console in Ruby on Rails

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.

How to create model rb files for all the tables in Mysql

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

Updating models in rails / migrations

Let's say I used the following command to create a "User" model:
script/generate model User username:string
This creates the user.rb file along with the migration rb file to create the Users table. Now, I want to add an email column to my User model. What's the best way to do that? Do I do it manually and write the migration file by hand or is there a shortcut for doing it? If I write the migration by hand, do I have to name it the same way as the previous migration script (with a timestamp in front) to guarantee that it runs after the previous migration?
Don't worry about the timestamp. It will be generated automatically.
You can do a
rails generate migration add_email_to_user email:string
This would automatically create a migration file which would look like this:
class AddEmailToUser < ActiveRecord::Migration
def self.up
add_column :email, :string
end
def self.down
remove_column :email
end
end
the file would have the timestamp in the format YYYYMMDDHHMMSS (For Rails 2.1 and above) appended in front of the filename.
The Guide has information about generating migrations. If you use the rails generator, it will create correctly named files:
ruby script/generate migration AddEmailToUser email:string
Well you can do two things:
1) If you haven't deployed this anywhere yet, or you don't mind dumping the db and running your migrations again, then modify the file. Remove the tables from your db, and run db:migrate. Easy to do this in development.
2) If this app is in production, or you don't want to drop all your tables. Then create a new migration file. Then in this new migration add/modify/drop the column. Then run db:migrate and the new changes will take effect in your table. This is the best practice.
As for naming your migration, timestamps are used because rails will create a table that keeps track of the latest migrations ran. For this, it is better to use the timestamps. But if you choose, you can use your own convention instead of timestamps. Maybe name them 001_migration.rb, 002_migration.rb, etc.
Hope that helps.

Resources