Modify scaffold :string for :text

694 views Asked by At

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?

2

There are 2 answers

2
Florent Ferry On BEST ANSWER

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.

4
Arslan Ali On

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.