I created a model ruby script/generate model Article (simple enuff)
Here is the migration file create_articles.rb:
def self.up create_table :articles do |t| t.column :user_id, :integer t.column :title, :string t.column :synopsis, :text, :limit => 1000 t.column :body, :text, :limit => 20000 t.column :published, :boolean, :default => false t.column :created_at, :datetime t.column :updated_at, :datetime t.column :published_at, :datetime t.column :category_id, :integer end def self.down drop_table :articles end end
When I run the rake:db migrate command I receive an error rake aborted! "Uninitialized constant CreateArticles."
Does anyone know why this error keeps happening?
To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.
Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema. rb file to match the structure of your database.
A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.
Every Rails app has a special directory— db/migrate —where all migrations are stored. Let's start with a migration that creates the table events into our database. This command generates a timestamped file 20200405103635_create_events. rb in the db/migrate directory.
Be sure that your file name and class name say the same thing(except the class name is camel cased).The contents of your migration file should look something like this, simplified them a bit too:
#20090106022023_create_articles.rb class CreateArticles < ActiveRecord::Migration def self.up create_table :articles do |t| t.belongs_to :user, :category t.string :title t.text :synopsis, :limit => 1000 t.text :body, :limit => 20000 t.boolean :published, :default => false t.datetime :published_at t.timestamps end end def self.down drop_table :articles end end
It's possible to get the given error if your class names don't match inflections (like acronyms) from config/initializers/inflections.rb
.
For example, if your inflections include:
ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'DOG' end
then you might need to make sure the class in your migration is:
class CreateDOGHouses < ActiveRecord::Migration[5.0]
rather than:
class CreateDogHouses < ActiveRecord::Migration[5.0]
Not super common, but if you generate a migration or a model or something, and then add part of it to inflections afterwards it may happen. (The example here will cause NameError: uninitialized constant CreateDOGHouses
if your class name is CreateDogHouses
, at least with Rails 5.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With