Search code examples
ruby-on-railsdatabaserails-migrations

Added Column in Rails, Data isn't recorded


I have been having the same problem for a month and cannot find a solution.

Whenever I add a column to my database, the column does not record information. I can pass information into it in my form, but that will never return.

Validations return an error, as if that field of the form was empty.

I have experimented with db:rollback, drop/create/migrate, and others.

Here is my initial migration, everything works fine:

class CreateRequests < ActiveRecord::Migration[5.0]
  def change
    create_table :requests do |t|
      t.string :library
      t.string :librarian
      t.string :program
      t.string :email
      t.string :phone
      t.string :date
      t.string :time

      t.timestamps
    end
  end
end

Here are my two added migrations:

class AddAddressColumnToRequests < ActiveRecord::Migration[5.0]
  def change
    add_column :requests, :address, :string
  end
end

and

class AddConfirmationColumnToRequests < ActiveRecord::Migration[5.0]
  def change
    add_column :requests, :confirmation, :boolean
  end
end

This has been my bane. Let me know what else to provide. Thank you.


Solution

  • Make sure you are allowing address & confirmation in the strong params. The code should look like:

    private
    # Using a private method to encapsulate the permissible parameters is
    # a good pattern since you'll be able to reuse the same permit
    # list between create and update. Also, you can specialize this method
    # with per-user checking of permissible attributes.
    def request_params
      params.require(:request).permit(:library, :librarian, :program, :email, :phone,
                                        :date, :time, :age, :address, :confirmation)
    end