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.
Related
I created the following migration:
class AddTokenToRegionTriggers < ActiveRecord::Migration[5.0]
def change
add_column :region_triggers, :token, :string
add_index :region_triggers, :token, unique: true
end
end
There's a table called region_triggers. I tried running rake db:migrate:up VERSION=123, but it didn't do anything. The output is:
Enabling Bootsnap
Model files unchanged.
When I ran rake db:migrate:redo VERSION=123, it said No indexes found on region_triggers with the options provided..
How do I make rails add the column and the index?
Where did you get the version number from? Did you create the migration file using rails g migration ?
If so, it would have created the migration file with a version number prefixing the model name, so (for example)
rails g migration AddTokenToRegionTriggers
Would generate
20180501113025_add_token_to_region_triggers.rb
That initial numeric string is the version number. To migrate up to that version, you can do
rake db:migrate:up VERSION=2018051113025
If you have no later migrations, you could just do...
rake db:migrate
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
rails g model Rating user_id:integer message:string value:integer
How can I completely remove this model? Thanks
When you generate a model, it creates a database migration. If you run 'destroy' on that model, it will delete the migration file, but not the database table. So before run
bundle exec rails db:rollback
rails destroy model <model_name>
For rails versions before 5.0 and higher use rake instead of rails
bundle exec rake db:rollback
rails destroy model <model_name>
Try this
rails destroy model Rating
It will remove model, migration, tests and fixtures
For future questioners: If you can't drop the tables from the console, try to create a migration that drops the tables for you. You should create a migration and then in the file note tables you want dropped like this:
class DropTables < ActiveRecord::Migration
def up
drop_table :table_you_dont_want
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
To remove migration (if you already migrated the migration)
rake db:migrate:down VERSION="20130417185845" #Your migration version
To remove Model
rails d model name #name => Your model name
Here's a different implementation of Jenny Lang's answer that works for Rails 5.
First create the migration file:
bundle exec be rails g migration DropEpisodes
Then populate the migration file as follows:
class DropEpisodes < ActiveRecord::Migration[5.1]
def change
drop_table :episodes
end
end
Running rails db:migrate will drop the table. If you run rails db:rollback, Rails will throw a ActiveRecord::IrreversibleMigration error.
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.
I was wondering if anyone knew how to update the files (adding/removing/updating an attribute) produced by using the scaffold generator in ruby on rails.
For example:
scaffold student name:string lastname:string
so this will create a the associate files (controller,view,etc) with name and lastname as string attributes. When you db:migrate the project, it'll create the table in the database. However, say I want to update whether it be update it with an addition attribue (ex. studenId:integer) or if its removing or updating an attribute, how do you do that?
I tired just updating the generated files, but when I do that db:migrate it still sets the schema that is generated to what is in the table. Is there a built in script in rails that will update the table?
Any advise appreciated?
Thanks,
D
Full command in this example:
$ rails generate migration add_studentid_to_student
You need new migration file for new attributes, from console:
$ script/gnerate migration add_sudentid_to_sudent
it will generate your_app/db/migrate/8293898391_add_sudentid_to_sudent.rb, spicify in this file your new attributes:
def self.up
add_column :sudents, :studentId, :integer
end
def self.down
remove_column :students, :studentsId
end
after that, back to console:
$ rake db:migrate
and than you can edit your views, model, controller files and use new attribute
Hi Try ruby script/destroy scaffold student and then ruby script/generate scaffold student
also try reading up on rails migrations, for dropping/updating table columns.
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html