Rails rake db:migration do not recognise new migrates - ruby-on-rails

strange problem with rails migration.
I have 2 migrations in my app
I run rake db:migrate
Only one migration executed. No matter how many time I tried, the second migration was ignored
I also tried to run the specific migration by specifying a version number, but no luck
Use rails 4.1
The first migration generated by
rails g scaffold User .......
The second migration generated by devise plugin
rails g devise user ........

The migration file generated is without extension .rb.
It was an issue that was already reported in Devise. It was resolved in Devise version 3.2.3. Read the issue Generated migration filename missing extension #2971

Second file needs the .rb extension?

Related

Rails - Table still in schema.rb after rails destroy scaffold and drop_table migration

I'm trying to remove an old model etc., that I had created by scaffolding several migrations past. So I followed the advice in this thread and ran a drop_table migration and the rails d scaffold Modelname command (maybe I did that first, not sure anymore).
As far as I can tell that removed the table and all the other files that had been created by the scaffolding, but my issue is that the schema.rb still includes the table! I've read here that I could probably fix this issue by running rails db:drop db:create db:migrate, but I was wondering if there is a way to fix schema.rb without resetting the database completely? (Unfortunately I started populating my db by console while I was adding parts to my app instead of writing a proper seed file.)
you can drop the table from rails console, for example the model is user then your table will be users
$rails console
Then just type:
ActiveRecord::Migration.drop_table(:users)
Additional answer for user problem:
to update table schema in schema.rb you can use
$ rake db:schema:dump

Ruby on rails updating model via rails console

I have run rails g model Task description:text. Then I have run rails console and put in a few tasks. I would now like to add more attributes to the create_tasks.rb file. Such as .string :title.
What I tried:
opened the file(create_tasks.rb), put the new line in. Then ran rake
db:migrate then went back into the console and opened the first task
and it doesn't show the title attribute.
also tried creating a new task using the title attribute. Error:
unknown title attribute for Task.
So, how do I update the model?
Welcome to Rails!
Here you can find some tutorials about how to deal with migrations:
http://edgeguides.rubyonrails.org/active_record_migrations.html
https://www.tutorialspoint.com/ruby-on-rails/rails-migrations.htm
Basically, every time you want to modify a migration, you must:
run rake db:rollback
modify the migration
run rake db:migrate
I hope this helps. Good coding!!
You need to rails db:rollback to roll the database back before the latest migration, add the new variables to the migration file, then run rails db:migrate to have the new parts of the migration file included.
If you need to roll back more revisions (if you have created more migrations since you created this model), you can either include the number of roll backs like
rails db:rollback STEP=<enter number of steps>
#e.g. rails db:rollback STEP=2
or, you could also rails db:reset which would remove all databases, recreate them, then remigrate them. Or you could rails db:drop to drop the database, then rails db:create and rails db:migrate to migrate the new database.
Do not edit the schema file. The schema file is automatically updated when you run migrations etc to match the content of your migration files.

Updating scaffold views after adding new column

I created a table using scaffold. Later added a new column
$ rails generate migration add_column_name_to_table_name column_name:string
$ rake db:migrate
Now I'm wondering is there any way to auto-update earlier generated by scaffold views?
Nope, you've got to do it manually.
FWIW, these sorts of changes are made much easier if you're using one of the form generator gems.

Ruby on Rails : purpose of db:migrate

When I read Rails book, each time they create a new database, always follow a db:migrate.
rails generate scaffold school
rake db:migrate
In console view, I see at first line, Rails create some files, no problem. but in second line, I see that Rails isn't really change anything. I have view some files that Rails nearly create and see no change too.
So, what the purpose of line 2, please tell me.
Thanks :)
The rake migrates the changes into your database. It is which acttually changes the database schema to match your previously generated scaffolded model.
Without it, you wouldn't have a table to write your objects into. Or in case of changed model, the table could differ from your model, leading to error.
When you generate a model (or scaffold one) a migration file is created in your db/migration directory. It is a pure text file, you can create such manually, if you want. This is the tool for the iterative development in rails regarding the database. Each migration adds some change to the system. When you run rake db:migrate your database is updated by the given migrations. This is a handy tool in case of distributed development, when one programmer can check out the code from the repository, and can run the migrations on his own development database.
db:migrate, is the command that tells rails to update the database with new changes. Think of it as this way
when u say rails generate scaffold rails will generate files like a model, controller etc.. and it create a file under db/migrate which has the sql script to update the database.
Ex: if you run rails generate scaffold User name:string, then you will need a table called users in the database with the column 'name', that sql script will generated under db/migrate folder
with db:migrate, command, you are telling rails to migrate new sql scripts to the database, in the above case, it will creates the 'users' table
if you run rake -T, from your rails application root, you could see all the rake tasks
HTH :)

Ruby on Rails: rails generate migration is not giving me a new migration, but giving me an app called generate

I typed this into terminal:
rails generate migration CreateAddress
and instead of creating a new migration file, it created the entirety of a naked rails app.
What is wrong here?
The generate script is a Ruby script, so you should just call it with ruby.
Also, you usually want to call that script from the top level of your app, so:
$ ruby script/generate migration CreateAddress
The reason you have your issue is because executing rails simply creates a naked Rails app in your current directory with the first argument as its name. In this case, that's obviously "generate".

Resources