user2284348
user2284348

Reputation: 43

Modify scaffold :string for :text

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?

Upvotes: 1

Views: 711

Answers (2)

Arslan Ali
Arslan Ali

Reputation: 17802

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.

Upvotes: 0

Florent Ferry
Florent Ferry

Reputation: 1387

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.

Upvotes: 1

Related Questions