I use this command to create an avatar column in my postgresql database.
rails g migration add_avatar_to_users avatar:string
rake db:migrate
I have an image column i would like to get rid of in the same database. I don't know the command to do so. I dont need the data and that column anymore.
I tried
rails destroy migration add_image_to_users image:string
rake db:migrate
But it did not fix the problem =/
rails g migration remove_image_from_users
That will generate a new migration, and then inside the change method you can write:
remove_column :users, :image
Related
I have a scaffold, but it fails because the text of the users is longer than string permits. So I would like to change the kind of data, rails g scaffold Dreams Dream:string for Dreams:text,
It is possible?
If you have already migrate, undo it:
rake db:rollback
rails destroy scaffold Dreams Dream:string
And redo it
rails generate scaffold Dreams Dream:text
rake db:migrate
You don't need to make rake db:rollback and rake db:migrate if you have just generated your scaffold.
If it is not your last migration, you can undo it with:
rake db:migrate:down VERSION=<version>
# version is the number of your migration file you want to revert
You can create a new migration:
rails generate migration change_dream_type_in_dreams
and open migration to use change_column
def self.up
change_column :dreams, :dream, :text
end
def self.down
change_column :dreams, :dream, :string
end
Finally, rake db:migrate.
First of all, the scaffold should be a singular noun like User and in your case, it should be Dream, Rails will not allow Dreams unless you pass --force-plural option.
Second, the column name should also be singular, though it can be plural, but rails convention in general is to have singular column names.
And yes, you are right!
rails g scaffold Dream dream:text
text is the option you are looking for. And if you do not specify anything with dream, Rails will take it as string.
I would like to rename a table column from the rails console without writing any migration.
how can I do that?
I opted to run this from the console:
ActiveRecord::Base.connection.rename_column :tablename, :old_column_name, :new_column_name
rails dbconsole
ALTER TABLE name RENAME column TO column
you run in console: rails g migration ChangeColName
you edit the file db/migrate/"timestamp"_change_col_name.rb insert in def change -
rename_column :tablename, :old_column_name, :new_column_name -save
you run in console: rake db:migrate :-)
I'm very new to ruby on rails, been trying to play around with it in the past few days.
Basically trying to: Create a empty table, with fixed columns - Ruby on Rails
I've created a model like so:
rails g model table
rails g migration table
my tables.rb files looks like this:
class Tables < ActiveRecord::Migration
def change
add_column :table, :firstname, :string
add_column :table, :lastname, :string
end
end
(hopefully I created the columns okay)
I then run:
rake db:migrate RAILS_ENV=development
but seem to get an error no such table: table (but I thought I created it ? )
Also what is a good view I can use to see my table on localhost:3000, in a html.erb file?
What you are displaying as your tables.rb file is a migration file, not a model. Models are stored in app/models. Migrations are in db/migrate and have a name that is a datetime stamp followed by the migration name.
Your migration is performing add_column. You can't add_column until you create_table. That migration should have been built using "rails g model table". Please show all migrations with the entire filename.
Check the document that dax provided. The rails generate command uses a stylized command line. Many standard migration functions, such as creating tables and adding columns, can be automatically generated by using the correct migration name. For example:
rails g migration add_url_to_feed url:string
This will create a migration that adds a string column called url to the feed table.
Generally, migrations should do what you need. However there is another command, rake, that you will need. The reference is here. For example:
rake db:create # Create the database from config/database.yml for the current Rails.env
can create the database for you.
Hi I have a general migration problem:
When I create migrations like this:
class RenameColumn < ActiveRecord::Migration
def change
rename_column :users, :hotel_stars, :rating_stars
rename_column :users, :restaurant_stars, :price_stars
end
end
and change the code in the Model-,View- and Controller file accordingly(I dont create new Model etc.):
ie.
Model: attr_accessible :rating_stars, :price_stars
(instead of :hotel_stars, :restaurant_stars )
Controller: #rating = current_user.rating_stars
When I now run the migration (rake db:migrate) -> it works!
But after a rake db:drop, rake db:create, rake db:migrate it doesn't anymore!
What is wrong with this migration? How can you create migrations that are working WITH and WITHOUT resetting the database?
Thanks!!
I think your issue is that rake db:create does not rebuild your database from schema.rb. For that you need to do rake db:setup instead of rake db:create. In any case I would try rake db:reset instead of drop/create as I believe that will accomplish what you want to do in one step.
Type rake -T for a list of available tasks and what they do.
Also see here for more information on rails migrations:
http://guides.rubyonrails.org/migrations.html
Did you ever manually alter the state of your database and/or modify a migration after it was run? If so, you messed up the state of migrations.
You "could" alleviate this by adding a migration that gets your database where your new migration expects it to be, or you could fix the migration that was altered before.
Depending on how many other developers you have, I would go for the latter in this case.
Is there a quick console command I can run to change the type I have for an object? It's currently a Ruby Date type, but I would like it to be a Ruby Time type.
I started with this scaffold command:
$ rails generate scaffold Post title:string content:text postdate:date
But wish I would have done the following:
$ rails generate scaffold Post title:string content:text postdate:time
Is there a command and can run to make the update?
Sometimes you have to write some actual code, even in Rails. Try creating migration and then using change_column method. Something like
change_column :my_table, :my_column, :new_type
You put this in your db migration file, not in shell.
If you don't want the mistake to be a permanent part of the migration set, simply migrate down (rake db:rollback), edit your migration file, and migrate back up (rake db:migrate).
Edit: To answer your question about there being a single command? Yes, there is. After editing your migration:
rake db:migrate:redo
This runs the "down" followed by the "up" in just one command.
Three steps to change the type of a column:
Step 1:
Generate a new migration file using this code:
rails g migration sample_name_change_column_type
Step 2:
Go to /db/migrate folder and edit the migration file you made. There are two different solutions.
def change
change_column(:table_name, :column_name, :new_type)
end
2.
def up
change_column :table_name, :column_name, :new_type
end
def down
change_column :table_name, :column_name, :old_type
end
Step 3:
Don't forget to do this command:
rake db:migrate
I have tested this solution for Rails 4 and it works well.