Why am I getting an error when migrating? Uninitialized constant AddDates - ruby-on-rails

I want to add a new column for a bunch of different items on my site.
class AddCreatedatToStreamItems < ActiveRecord::Migration
def change
add_column :stream_artworks, :createdat, :date
add_column :stream_experiments, :createdat, :date
add_column :stream_photographies, :createdat, :date
add_column :stream_webs, :createdat, :date
add_column :stream_socials, :createdat, :date
end
end

Any chance the filename of the migration is "123123_add_dates.rb" instead of "123123_add_createdat_to_stream_items.rb" (where '123123' is a string of digits, not necessarily '123123')?
The classname of the migration and the filename need to match in the CamelCase => camel_case way.

Related

rake migration error MaBase is not a class

I don't found anything on this issue.
I create migration like this :
rails generate migration MaBase
And I fill the *_ma_base.rb like that
class MaBase < ActiveRecord::Migration
def change
create_table :connexion
add_column :connexion, :ip, :string
add_column :connexion, :user_agent, :string
add_column :connexion, :nb, :integrer
add_column :connexion, :date, :datetime
create_table :recherche
add_column :recherche, :ip, :string
add_column :recherche, :recherche, :string
add_column :recherche, :date, :datetime
create_table :membre
add_column :membre, :ip, :string
end
end
And I get the error.
After reading some subject here, and i create create a file in model ma_base.rb with this kind of code :
class MaBase < ActiveRecord::Base
end
But the error is stil here
Thanks for support
Change your class name in migration file from MaBase to MaBases. Activerecord maps plural of model names in db files. Try this:
class MaBases < ActiveRecord::Migration

Remove boolean and add list in ruby on rails

I would like to modify gender field,
Initially i have declared gender field as boolean true or false. but now i want it to be changed as list (Male, Female, Other).
class AddExtraFieldsToUser < ActiveRecord::Migration
def change
add_column :users, :phone_number, :string
add_column :users, :date_of_birth, :datetime
add_column :users, :gender, :boolean, default: false
add_column :users, :live_in, :string
add_column :users, :description, :text
end
end
Can i modify as following.... please let me know the correct way...
i thought of doing rails g migration RemovegenderFromUsers gender:boolean
then rake db:migrate followed by creating new one
rails g migration AddGenderToUsers gender:select
user.rb
GENDER_TYPES = ["Male", "Female", "Other"]
html
<%= f.select :gender, User::GENDER_TYPES %>
Is above mentioned process correct or any other way ?
The answer by Ahmad Hussain is correct . List is not a database field type .
You should generate a migration to change the column type :
**change_column :table_name, :column_name, :type**
Select is not a database field type if you want to do it then do it like this
rails g migration AddGenderToUsers gender:integer
In migration file change it to like this:
change_column :users, :gender, :integer, default: 0
For form page do this:
<%= f.select :gender, User::GENDER_TYPES.each_with_index.map { |gender, index| [gender, index] } %>
And in user model you can define function to get gender name to display
def gender_name
GENDER_TYPES[gender]
end

Rails: change column type, but keep data

I have a model with a column of type integer which I want to convert to type string. Now I'm looking for the best way to change the column type without losing the data. Is there a painless way to accomplish this?
A standard migration using the change_column method will convert integers to strings without any data loss. rake db:rollback will also do the reverse migration without error if required.
Here is the test migration I used to confirm this behaviour:
class ChangeAgeToString < ActiveRecord::Migration
def self.up
change_column :users, :age, :string
end
def self.down
change_column :users, :age, :integer
end
end
for postgres
in migration
change_column :table_name, :field,'boolean USING (CASE field WHEN \'your any string as true\' THEN \'t\'::boolean ELSE \'f\'::boolean END)'
and to any valid type similar
for postgresql, change table column datatype integer to string,
rails migration like this with up and down actions
class ChangeAgeToString < ActiveRecord::Migration
def self.up
change_column :users, :age, 'varchar USING CAST(age AS varchar)', null: false
end
def self.down
change_column :users, :age, 'integer USING CAST(age AS integer)', null: false, default: 0
end
end
If it's a one-off you can just change the column type in the database (since no info is lost moving from int to varchar)
For MySQL, this would do it:
ALTER TABLE t1 MODIFY col1 VARCHAR(256)
If you're using SQLite you won't have to do anything.
You can try something like this:
change_column :table_name, :column_name, 'integer USING CAST(column_name AS integer)'
or even better:
change_column :table_name, :column_name, :integer, using: 'column_name::integer'
You can read more about this topic here: https://kolosek.com/rails-change-database-column
If you use Postgres, you can't implicitly cast a string back to an integer, so the way to make the change reversible is:
class ChangeAgeToString < ActiveRecord::Migration
def self.up
change_column :users, :age, :string
end
def self.down
add_column :age_integer
User.connection.execute('UPDATE users SET age_integer = cast(age as int)')
remove_column :users, :age
rename_column :users, :age_integer, :age
end
end

Migrations: change column from integer to string

Can anyone show me how to edit the following migration to change :phone integer to string?
class CreateContactInfos < ActiveRecord::Migration
def change
create_table :contact_infos do |t|
t.integer :phone
t.string :facebook
t.references :user
t.timestamps
end
add_index :contact_infos, :user_id
end
end
Thanks in advance!
I guess you already migrated the one you're showing, so create another in which you'd put:
change_column :contact_infos, :phone, :string
I have added some more explanation to this.We need to generate a new migration
rails g migration change_phone_to_be_string_in_contact_infos
If we open up the migration we should see something like this
class ChangePhoneToBeStringInContactInfos < ActiveRecord::Migration[5.0]
def change
end
end
What we call this migration will have no impact on what we need to do next, but future we and other developers will thank us for naming our migration appropriately.
As you can see the change method is sitting empty. We need to manually add some code here.
class ChangePhoneToBeStringInContactInfos < ActiveRecord::Migration[5.0]
def change
change_column :customers, :phone, :string
end
end
After Saving this file just do rake db:migrate we can see changes we want.
For a reversible migration, use:
def up
change_column :contact_infos, :phone, :string
end
def down
change_column :contact_infos, :phone, :integer, using: "phone::integer"
end
Convert column type string into integer in rails migration :
def change
change_column :contact_infos, :phone, :integer, using: 'phone::integer'
end
Convert column type integer into string in rails migration:
def change
change_column :contact_infos, :phone, :string, using: 'phone::string'
end

How do I change column type in Heroku?

I am trying to rake the db:migrations into my heorku instance and I get an error. The FAQ described my error as below:
Cannot change column type
Example: PGError: ERROR: column
“verified_at” cannot be cast to type
“date”
Cause: PostgreSQL doesn’t know how to
cast all the rows in that table to the
specified type. Most likely it means
you have an integer or a string in
that column.
Solution: Inspect your records and
make sure they can be converted to the
new type. Sometimes it’s easier to
just avoid using change_column,
renaming/creating a new column
instead.
How do I change this migration now. This is the problem that I have. For my Contacts table, I created the following:
t.string :date_entered
In a later migration, I do the following:
change_column :contacts, :date_entered, :date
This change_column appears to be the problem.
Should I...change by hand that migration? Is there a way I can clean the data in my tables (I didn't know Heroku would recognize the data in the table because I'm doing a rake).
I obviously need to change this value and it is used throughout my application. Thanks.
This is what I am trying...thoughts?
def self.up
#change_column :contacts, :date_entered, :date
#this fails in postgres, so trying the same outcome
rename_column :contacts, :date_entered, :date_entered_old
add_column :contacts, :date_entered, :date
remove_column :contacts, :date_entered_old
end
def self.down
add_column :contacts, :date_entered_old
remove_column :contacts, :date_entered
rename_column :contacts, :date_entered_old, :date_entered
end
Do the following:
rename the column A
create the new column B as date
move the data from A to B
remove A
In other words
def self.up
rename_column :contacts, :date_entered, :date_entered_string
add_column :contacts, :date_entered, :date
Contact.reset_column_information
Contact.find_each { |c| c.update_attribute(:date_entered, c.date_entered_string) }
remove_column :contacts, :date_entered_string
end
This is a modified and tested version of Simone Carletti's solution
class ModifyContacts < ActiveRecord::Migration
def self.up
rename_column :contacts, :date_entered, :date_entered_string
add_column :contacts, :date_entered, :date
Contact.reset_column_information
Contact.find(:all).each { |contact| contact.update_attribute(:date_entered, contact.date_entered_string) }
remove_column :contacts, :date_entered_string
end
end

Resources