Undefined add_column in Rails 3 - ruby-on-rails

I am trying to run a migration in Rails 3, I wish to add a column to a table, the code looks like this:
class AddConstAdr < ActiveRecord::Migration
def change
change_table: constants do |t|
t.add_column :home_address, :string
end
end
end
When I do rake db:migrate I get an error saying undefined method 'add_column'. I am confused as to why this is happening, can anyone help?

You seem to be mixing two different ways of doing a migration. You probably want this:
def change
change_table :constants do |t|
t.string :home_address
end
end
or this:
def change
add_column :constants, :home_address, :string
end
Both forms should do the same thing: add a home_address string column to the constants table.
I'm also assuming that your change_table: constants is just a typo that should have been change_table :constants.
Further information may be found in the Migrations Guide.

You should do as below:
def change
add_column :constants, :home_address, :string
end

Related

Why is this migration irreversible? (change_table, rename, text)

I have what I think is a pretty simple migration. For some reason I get an IrreversibleMigration error when I try to db:rollback or db:migrate:redo.
The migration runs smoothly, but I'd rather keep it reversible. I can't figure out why it's not as written. Any ideas?
Here's the migration:
class AddWhyHypAndWhyHypeToStatements < ActiveRecord::Migration
def change
change_table :statements do |t|
t.rename :description, :why_hypocritical
t.text :why_hypothetical
end
end
end
If it matters, "description" column is a text column. I'm using Rails 3.1/Ruby 1.9.2/PostgreSQL. Thanks for any help.
Looks like Rails has troubles reverting change_table method. Try doing it that way instead:
class AddWhyHypAndWhyHypeToStatements < ActiveRecord::Migration
def change
rename_column :statements, :description, :why_hypocritical
add_column :statements, :why_hypothetical, :text
end
end
You can see the list of commands that can be inverted in the docs or in Rails Guides.

Rails Globalize3 gem: How do I add an additional field to the translation table using a migration?

The docs for the Globalize3 gem are clear about how to create a translation table, but I don't see any information about how to add a field to a translation table during a later migration. For example, I initially included Category.create_translation_table! :name => :string when I created my Category model. Now, however, I need to add a translated field to the model.
How do I do that with a Rails migration? I don't see any docs for an alter_translation_table! method or anything similar...
You can do it by hand, something like the following:
class AddNewFieldToYourTable < ActiveRecord::Migration
def self.up
change_table(:your_tables) do |t|
t.string :new_field
end
change_table(:your_table_translations) do |t|
t.string :new_field
end
end
def self.down
remove_column :your_tables, :new_field
remove_column :your_table_translations, :new_field
end
end
With Globalize4, just :
class AddHintToCategory < ActiveRecord::Migration
def up
Category.add_translation_fields! hint: :text
end
def down
remove_column :category_translations, :hint
end
end
Don't forget to add the new field in your model :
translate :name, :hint
https://github.com/globalize/globalize/blob/master/lib/globalize/active_record/migration.rb:34 line (globalize 4)
add_translation_fields!(fields, options)
P.S. Just a typo in a previous comment, 'add_transaction_fields' isn't defined.

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

Rails won't let me change records during migration

This must be something simple but it's driving me nuts!
I have a migration where I want to update a record afterward
class SubjectsTextField < ActiveRecord::Migration
def self.up
add_column :users, :subjects, :text
User.find(39).update_attribute :subjects, "hey there"
end
def self.down
remove_column :users, :subjects
end
end
The column gets created but when I go to check record 39, it's subjects field is null and doesn't say "hey there". No errors are thrown during the migration and the update_attribute line returns true as if it had worked.
This line works perfectly in the console and has the expected effect:
User.find(39).update_attribute :subjects, "hey there"
I tried putting the update_attribute line in a second migration. If I blow through both of them in one "rake db:migrate" all the way to current, it still doesn't work.
But here is the weird part. If I run two separate migrations, say "rake db:migrate VERSION=10" to only create the column and then the second one with "rake db:migrate" to update the attribute IT WORKS!
What the heck is going on...how do I modify a record during a migration? I seem to remember doing this quite often in the past. Maybe it is something different with Rails 2.3.2?
Thanks!
Brian
You need to call reset_column_information on the model you changed before you can use the new column. Add this between the add_column and update:
User.reset_column_information
See "Using a model after changing its table" on the ActiveRecord::Migration page.
This syntax is much clear...try with change_table
class AddReceiveNewsletterToUsers < ActiveRecord::Migration
def self.up
change_table :users do |t|
add_column :users, :subjects, :text
end
User.find(39).update_attribute "subjects", "hey there"
end
def self.down
remove_column :users, :receive_newsletter
end
end
If you combine the two in your initial migration like this, does that work?
class SubjectsTextField < ActiveRecord::Migration
def self.up
add_column :users, :subjects, :text
User.find(39).update_attribute "subjects", "hey there"
end
def self.down
remove_column :users, :subjects
end
end

Alter existing model with a Redmine plugin

The Redmine plugin tutorials explain how to wrap core models but what I need is to add another column to the journals table.
I need a boolean field inserted in the journals model. Creating another model with a 'belongs_to :journal' relation seems like an overkill.
Can this be done with a plugin?
I should note that I am a rails newbie.
You just have to create the appropriate migration.
In your plugin's directory, create the file db/migrate/update_journal.rb with the following :
class UpdateJournal < ActiveRecord::Migration
def self.up
change_table :journal do |t|
t.column :my_bool, :boolean
end
end
def self.down
change_table :journal do |t|
t.remove :my_bool
end
end
end
Then you can execute the task rake db:migrate_plugins RAILS_ENV=production to update your database with the new field.
After executing the migration, your journal database will have the my_bool field that you'll be able to call like every other field.
I was able to extend the existing user model using the following code:
class UpdateUsers < ActiveRecord::Migration
def up
add_column :users, :your_new_column, :string, :default => ''
add_column :users, :your_other_new_column, :string, :default => ''
end
def down
remove_column :users, :your_new_column
remove_column :users, :your_other_new_column
end
end
Also I needed to name the migration file in way that it began with a number eg. myplugin/db/migrate/001_update_user.rb

Resources