I jus created a rails application. I created a model using ruby script/generate model Article
next i edited the my 001_create_articles.rb file by adding these lines in self.up method
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.string :published_at
t.timestamps
end
end
Now i ran rake db:migrate . But migrate does not work, it simply does no print anything. Anyone knows where i am going wrong?
Are you missing one end from your up method?
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.string :published_at
t.timestamps
end
end
I think you have to generate a migration. If i understood it right, you added the migration code to the model.
You have to run something like that:
ruby script/generate migration articles
After that open the generated file and add your code there. Hope it helps
Related
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
I currently have the migrate thing like:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :encrypted_password
t.string :salt
t.timestamps
end
end
end
now, if I wanna add two new attributes into this file, one is: t.string :type , and the other one is: t.string :memory_token , how can I do this please?
If you have already run the migration you will have to create a new one.
rails g migration AddTypeToUsers
And then in the migration file you can edit in
change_table :users do |t|
t.string :type
t.string :memory_token
end
Then run a migration rake db:migrate to make the changes
If you haven't run the migration then you can simply add
t.string :type
t.string :memory_token
To that file you have showed us and then run your migration
+1 to #JTG
You can also make this just with one line:
rails g migration AddTypeAndMemoryTokenToUsers type:string memory_token:string
and you will get a following file:
class AddTypeAndMemoryTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :type, :string
add_column :users, :memory_token, :string
end
end
which will make the changes after running rake db:migrate
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
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!
My sqlite3 database works fine in development but when I try to migrate it to production I get the following error:
PG::Error: ERROR: relation "movies" does not exist
: ALTER TABLE "movies" ADD COLUMN "production_company" character varying(255)/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `async_exec'
I know a few people have posted about this but nothing I've tried seems to work. Anyone know how I might fix this?
Here's the migration:
class AddProductionCompanyToMovies < ActiveRecord::Migration
def change
add_column :movies, :production_company, :string, :limit => nil
end
end
Here's my schema.rb file if this helps:
ActiveRecord::Schema.define(:version => 20130331014529) do
create_table "movies", :force => true do |t|
t.string "title"
t.string "actor_1"
t.string "locations"
t.string "release_year"
t.string "string"
t.string "actor_2"
t.string "actor_3"
t.string "writer"
t.string "director"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "production_company"
t.string "distributor"
t.string "fun_facts"
end
end
Here's the migration where I create the movies table:
class Movies < ActiveRecord::Migration
def up
end
def down
end
end
It's not the best approach but a quick fix would be to replace that migration with this:
class AddProductionCompanyToMovies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :production_company
t.timestamps
end
end
end
Your migration where you create the movie table is incorrect. The up and down methods you have don't do anything. Because of this, there is no movie table to add the production_company column to.
You need something like this;
class Movies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :title
t.string :actor
.
. #add your columns you want in your initial migration here
.
end
end
I can't say why things worked in development in SQLite but at some point you successfully created the movies table and then maybe you altered the migration after that. This is easy to do (I've done it!).
A lot of people recommend that when you set up production you don't run your migrations to set up the database, but instead use rake db:schema:load (in fact, if you read the comments at the top of your db/schema.rb file it specifically describes this).
Another point is that a lot of people recommend having the same database in development as in production as there are subtle differences that can lead to unexpected problems in production (this is not what has caused your issue). If you're only just starting out then don't worry about it for now; it's just one more headache to set up PostgreSQL on your own machine; but it's something to keep in mind as you progress.