When I push my rails 4 app to Heroku, I receive the following fatal rake error:
You're trying to register ActiveAdmin::Comment as Comment, but the existing ActiveAdmin::Resource config was built for Comment!
I have a Comment model which has an ActiveAdmin file like this:
# /app/admin/comment.rb
ActiveAdmin.register Comment do
end
An ActiveAdmin migration creates the following active_admin_comments table. I am wondering if this might be related to the conflict, but I am not sure.
class CreateActiveAdminComments < ActiveRecord::Migration
def self.up
create_table :active_admin_comments do |t|
t.string :namespace
t.text :body
t.string :resource_id, :null => false
t.string :resource_type, :null => false
t.references :author, :polymorphic => true
t.timestamps
end
add_index :active_admin_comments, [:namespace]
add_index :active_admin_comments, [:author_type, :author_id]
add_index :active_admin_comments, [:resource_type, :resource_id]
end
def self.down
drop_table :active_admin_comments
end
end
I did have ActiveAdmin working fine on Heroku before this, including both of the files above. I cannot think of what has changed since the last push that could affect the Comment model. Any points of direction would be greatly appreciated!
Related
I'm using rails 5, and I've installed the gem and tried to run the migration, but I'm getting this error:
Index name 'index_rates_on_rater_id' on table 'rates' already exists
Does anyone know why this exists? This is a new site and started fresh just adding devise gem.
This is the migration file that wouldn't complete on execution rails db:migrate
class CreateRates < ActiveRecord::Migration[5.1]
def self.up
create_table :rates do |t|
t.belongs_to :rater
t.belongs_to :rateable, :polymorphic => true
t.float :stars, :null => false
t.string :dimension
t.timestamps
end
add_index :rates, :rater_id
add_index :rates, [:rateable_id, :rateable_type]
end
def self.down
drop_table :rates
end
end
The gem creates a migration that does not work in later versions of rails. In Rails 5 when you use the belongs_to and references macros they create indices and foreign keys by default.
All you really need is this:
class CreateRates < ActiveRecord::Migration[5.1]
def self.change
create_table :rates do |t|
t.belongs_to :rater
t.belongs_to :rateable, polymorphic: true
t.float :stars, null: false
t.string :dimension
t.timestamps
end
add_index :rates, [:rateable_id, :rateable_type]
end
end
You don't need up and down since Rails is smart enough to know how to rollback this migration.
Hello I started my rails application using sqlite, however when I tried deploying it on heroku I found out that I needed to use postgreSQL. So I went through the trouble to change my gemfile and database.yml file and create the new postgresql database. However when I try to migrate my database I get the error:
ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR: type "reference" does not exist
LINE 1: ALTER TABLE "questions" ADD "quiz_id" reference
that is probably because I used a reference to make a relation in my db
I am basically looking for the fix for this situation.
Here are my migrations(if it matters):
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :question
t.string :answer1
t.string :answer2
t.string :answer3
t.string :answer4
t.integer :correct_id
t.timestamps null: false
end
end
end
class CreateQuizzes < ActiveRecord::Migration
def change
create_table :quizzes do |t|
t.string :name
t.string :subject
t.timestamps null: false
end
end
end
class AddQuizIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :quiz_id, :reference
end
end
Edit new question:
When on my heroku server there are some dead pages, but not when I am running on my local server. Here is my heroku address: https://krisquiz.herokuapp.com/
The dead pages are when you submit a question and when you try and start a quiz. I looked at the urls and they look properly. The only thing in common that I can think of for the two pages is that I built the urls manually for the links (ex: request.base_url + '/quiz/' + quiz.id.to_s + '/start'). As I am not sure what I need to give you as information just tell me and I will try to quickly get back to you.
Use integer type instead of references
add_column :questions, :quiz_id, :integer
Environment:
Windows 8.1
Ruby 2.1.5
rails 4.1.8
I have the following in one of my migration files:
class CreateVotesMigration < ActiveRecord::Migration
def change
create_table(:votes) do |t|
t.integer :item_id
t.integer :user_id
t.integer :vote
t.integer :vote_weight
t.string :vote_scope
t.string :slug
t.timestamps
end
add_index :votes, [:item_id, :user_id, :vote]
add_index :votes, :slug, unique: true
end
end
When I try to run the migration, I get the following error message:
rake aborted!
NameError: uninitialized constant CreateVotes
Any ideas?
Might be stemming from a routing error in your config/routes.rb file. Try checking if you have a route that has that name and change it to match your table.
Your filename need to match the Migration Class name. In your case,
class CreateVotesMigration < ActiveRecord::Migration
the filename would need to be 2014xxxxxxxxxx_create_votes.rb
I'm struggling here with the db migration for the acts_as_commentable_with_threading.
After generating the migration rails generate acts_as_commentable_with_threading_migration I proceeded to add the comment table rake db:migrate. Nothing happened, no error message, just returned to the regular prompt.
Looking at other response to this problem I tried rake db:migrate VERSION= # version number.
Yet here I get an Error response ActiveRecord::UnknownMigrationVersionError:
I must be doing something extremely wrong here since the computer doesn't validate the existence of my comment migration...
UPDATE from #Tiago answer
Ran rails generate migration acts_as_commentable_with_threading_migration
I had to manually create the migration by adding this code to the migration file. Then, thedb:migration worked perfectly.
Why wasn't it working in the first place? The Documentation clearly indicate to run rails generate acts_as_commentable_with_threading_migration.
Just create a migration for yourself as indicated in the gem's page:
rails g migration acts_as_commentable_with_threading_migration
And paste that to the file:
class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration
def self.up
create_table :comments, :force => true do |t|
t.integer :commentable_id, :default => 0
t.string :commentable_type
t.string :title
t.text :body
t.string :subject
t.integer :user_id, :default => 0, :null => false
t.integer :parent_id, :lft, :rgt
t.timestamps
end
add_index :comments, :user_id
add_index :comments, [:commentable_id, :commentable_type]
end
def self.down
drop_table :comments
end
end
I recently ran this migration while installing the fuzzily gem:
class AddTrigramsModel < ActiveRecord::Migration
extend Fuzzily::Migration
end
From looking at my schema.rb file, it looks like the effect of this migration was:
create_table "trigrams", :force => true do |t|
t.string "trigram", :limit => 3
t.integer "score", :limit => 2
t.integer "owner_id"
t.string "owner_type"
t.string "fuzzy_field"
end
add_index "trigrams", ["owner_id", "owner_type", "fuzzy_field", "trigram", "score"], :name => "index_for_match"
add_index "trigrams", ["owner_id", "owner_type"], :name => "index_by_owner"
Not sure if the easiest way is just to drop the table trigrams, or if there is a more appropriate method? I am assuming the indexes will be deleted on dropping the table?
Just run rake db:rollback. Fuzzily has support for rollbacks. Although everything it does is dropping the trigrams table :)
# lib/fuzzily/migration.rb:33
def down
drop_table trigrams_table_name
end