2

I am new to rails. I am using Rails 4.0.0 and ruby 2.0.0. I am learning through Ruby on Rails 4 essential training by Kevin Skoglund. He uses mysql in that, but I couldn't install it, so I switched to sqlite3 and everything is working properly.

PROBLEM: I am running rake db:migrate. And it seems to run properly, but I couldn't see the tables in db in sqlite3.

DETAILS: Created the db simple_cms_development using

sqlite3 simple_cms_development.db

Configuered the project in database.yml by setting the location of simple_cms_development to 'database:' in 'development:'. And then ran

rake db:schema:dump to connect rails to database and export schema.

Then I generated migration using model by

rails generate model User

Inside create_users.db

class CreateUsers < ActiveRecord::Migration

  def up
    create_table :users do |t|
      t.column "first_name", :string, :limit => 25
      t.string "last_name", :limit => 50
      t.string "email", :default => "", :null => false
      t.string "password", :limit => 40
      t.timestamps
    end
  end

  def down
    drop_table :users
  end

end

Then I ran migration rake db:migrate. I got:

==  DoNothingYet: migrating ================
==  DoNothingYet: migrated (0.0000s) =======

==  CreateUsers: migrating =================
-- create_table(:users)
   -> 0.1941s
==  CreateUsers: migrated (0.1951s) ========

I checked schema.rb, and it has:

ActiveRecord::Schema.define(version: 20140121101037) do

  create_table "users", force: true do |t|
    t.string   "first_name", limit: 25
    t.string   "last_name",  limit: 50
    t.string   "email",                 default: "", null: false
    t.string   "password",   limit: 40
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

Till now everything is working fine.

Then I open simple_cms_development.db in sqlite3, and try to see tables by .tables it shows nothing, no error. It shows just another sqlite prompt. It should show schema_migrations and users. What should I do?

anmolagrawal
  • 203
  • 1
  • 2
  • 12
  • 1
    Rails will use whatever database is specified in the `/config/database.yml` file - not the one you created from the command line. For SQLite databases, these will generally be stored under the `/db` directory. Can you check what is in your database.yml file? – PinnyM Jan 21 '14 at 16:59
  • Having a similar issue where a table that was the target of an add column migration was no longer accessible to rails tests. – Julien Lamarche Apr 08 '19 at 21:06

2 Answers2

3

from terminal go to your project directory and paste the following line:

sqlite3 -line db/development.sqlite3

then you will find the database console and now you can write your query here like:

sqlite> select * from users;

Emu
  • 5,763
  • 3
  • 31
  • 51
0

Reading this post, running

rails db:drop db:create db:migrate RAILS_ENV=test

fixed it for me.

Julien Lamarche
  • 931
  • 1
  • 12
  • 29