Heroku rake db:migrate failing - uninitialized constant - ruby-on-rails

My app is working fine locally and my push to Heroku was successful. But, when I run heroku run rake db:migrate, I get the following error:
NameError: uninitialized constant AddWeightToExercises
Here is the failed migration:
class AddWeightToExercise < ActiveRecord::Migration
def change
add_column :exercises, :weight, :float
end
end
edit: Thanks for the help everyone. The solution was to pluralize the class name to match the file name. Thanks for the help and quick responses.

Your migration file's name should correspond to AddWeightToExercises. It should be accordingly xxxxxxxx_add_weight_to_exercises, where xxxxxxx corresponds to a particular timestamp.

Your file name uses "exercises" plural, but your class name is AddWeightToExcercise singular. The two need to be consistent for rails to dynamically load the appropriate class.

Related

NameError: uninitialized constant Model - rake db:migrate

I'm trying to migrate a change to my Comments table with rails migration. I've already made the migration and everything looks good but when I run rake db:migrate it sends back this error NameError: uninitialized constant Model. I can't see the problem? All the naming look right to me. Here is my migration file let me know if anything looks off or if you need more information. Thank you!
MIGRATION:
class AddColumnToComments < ActiveRecord::Migration
def change
add_column :comments, :fav_drink, :string
add_column :comments, :visit_time, :string
end
end
ERROR:
I assume you've used rails generator to create your migration and this generator has probably create a migration file named: 201606xxxxxxxx_model.rb.
If it's the case, rename you migration file as follow: 201606xxxxxxxx_add_column_to_comments.rb

has_many method does not work after renaming model and table

I had modelA, which has_many modelBs and modelB belongs_to modelA. I tried to change the name of a modelB to modelC without really knowing what I'm doing. Before I could call #modelA.modelBs just fine and get a list of objects, but now I can't call #modelA.modelCs. I get a NoMethodError. These are the things I did to try to make the switch:
I renamed the table with a sqlite3 migration:
def change
rename :modelBs, :modelCs
end
Then I change modelA so that it has_many :modelCs and I renamed and edited modelB.rb (now modelC.rb)and changed the class name so that it's class ModelC < ActiveRecord::Base. It still belongs_to :modelA
Rails can't find a bars method to call on an instance of ModelA. I get a NoMethodError when I do #modelA.modelCs. I ran the migration, I restarted the server. What am I missing? Did I do a bad thing?
did the migration run successfully and did your table actually get renamed? I have always used rename_table for this:
def change
rename_table :modelBs, :modelCs
end
Inside your class, you can also tell rails which table the model should be associated with, just to test whether it's looking at the right table
class modelC < ActiveRecord::Base
table_name "modelCs"
end
hope that helps
What happens if you do a $rake db:reset? db:reset is supposed to rebuild the database with all of the seed data (without the migrations); maybe check this and see what the dependencies are like at this point, then you can run your $rake db:migrate to run the migrations and see what your left with there. Can you include more information about your backend? Is it Sqlite3? PostgreSQL?
db:migrate runs (single) migrations that have not been run yet.
db:reset does db:drop, db:setup

NoMethodError: undefined method reset_sent_at hartle tutorial

I'm doing chapter 10 of enter link description here. I got the same error mentioned in enter link description here
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method `reset_sent_at=' for #<User:0xccd47c0>
app/models/user.rb:66:in `create_reset_digest'
app/controllers/password_resets_controller.rb:12:in `create'
test/integration/password_resets_test.rb:17:in `block in <class:PasswordResetsTest>'
I try to do everything mentioned in the answer.
first thing I did was:
rails generate migration add_reset_to_users reset_digest:string reset_sent_at:datetime
and the answer was:
Another migration is already named add_reset_to_users:
so I'm sure I did the migration before.
This _add_reset_to_users.rb file in migrate folder.
class AddResetToUsers < ActiveRecord::Migration
def change
add_column :users, :reset_digest, :string
end
end
Then I try to restart my rails server.(I'm not sure if I'm doing it right) using
rails server
and then shutting down the server.
Non of them worked. I'm still getting the same error.
Another migration is already named add_reset_to_users:
You had already created the migration previously, as you noted, but this migration did not include the addition of the reset_sent_at column.
At this point the easiest thing to do would be to create a new migration to add the missing column.
rails generate migration add_reset_sent_at_to_users reset_sent_at:datetime

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR

I'm getting an error when trying to run rake db:migrate:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "plus_ids" of relation "comments" does not exist
I guess I did do some funky stuff when trying to remove the attribute plus_ids from my comment model. I'm not sure what is going on.
If I do a rake db:reset and then fetch the live database it all works locally again, but if I try to do a rake db:migrate it throws an error again.
How can I get rid of plus_ids properly so I can migrate?
Edit 1: Here's the migration files when I added (and removed) the attribute:
class RemovePlusIdsFromComments < ActiveRecord::Migration
def change
remove_column :comments, :plus_ids, :integer
end
end
And then
class AddPlusIdsToComments < ActiveRecord::Migration
def change
add_column :comments, :plus_ids, :integer
end
end
They're in the wrong order in the migration list, not sure why.
Edit 2: I think there is something wrong with the Up/Down, if I run rake db:migrate:status I get this:
down 20150305203336 Remove plus ids from comments
down 20150305204404 Add plus ids to comments
Any way around this?
The answer was to run
rake db:migrate:up VERSION=20150305204404
On both live and local, and then
rake db:migrate
On both again. Now the live and local database is in sync again, and migrating the database throws no errors.

Syntax error fixing devise to not use email

class UpdateIndexOnUsers < ActiveRecord::Migration
def up
sql = 'DROP INDEX index_users_on_email'
sql << ' ON users' if Rails.env == 'production' # Heroku pg
ActiveRecord::Base.connection.execute(sql)
end
end
Getting a syntax error when trying to rake db:migrate on heroku. Any help would be greatly appreciated. Using Postgresql. I should mention it works fine locally.
Edit: The error
PG::SyntaxError: Error: syntax error at or near "ON"
Line 1: DROP INDEX index_users_on_email ON users
^
The easy thing would be to use rails' helpers and let them take care of datebase specifics:
class UpdateIndexOnUsers < ActiveRecord::Migration
def up
remove_index :users, :email
end
end
If the index was initially created with a name other than the one that rails would use by default you could do
class UpdateIndexOnUsers < ActiveRecord::Migration
def up
remove_index :users, :email, :name => 'name_of_index'
end
end
As far as I know, Postgresql doesn't support (or need) ON xxxx when dropping an index. The docs don't mention it either:
http://www.postgresql.org/docs/9.2/static/sql-dropindex.html
Just specifying the index name should be fine, though you can qualify it with the schema name which you probably don't need to do if you've got a standard setup.
If you're also using postgresql locally, then presumably it's working because ON users is only added when rails env is production whereas you're presumably running in development locally.

Resources