Search code examples
ruby-on-railsformsscaffolding

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?


Solution

  • 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.