Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on rails from console - (Table doesn't exist)

I'm learning RoR in Windows 7 with InstantRails.

I got into sqlite3 successfully and created a table named Trades with a handful of columns. I got out of that and into ruby console.

>> class Trade < ActiveRecord::Base; end
=> nil
>> trade = Trade.new
=> #<Trade barterID: nil, title: nil, message: nil, created_at: nil, updated_at: nil>
>> trade.class
=> Trade(Table doesn't exist)

I double-checked that by getting back into sqlite3 and it's definitely there. I know the table isn't named "Trade" so I tried re-naming is as Trade, but it gave even more errors. I read that the table name should be in plural format, so I think I have that part right.

Any help on why it says the table doesn't exist? I'll give any details I haven't thought of.

like image 777
robbievasquez Avatar asked Dec 30 '25 16:12

robbievasquez


2 Answers

rails g model Trade will give you a correct template, but if you just want to fix your migration file, please make sure you create this table: trades (plural not singular).

Rails will give you (Table doesn't exist) error if you have trade(singular) table in the database. I think the error is kinds of misleading.

like image 144
Hugeidea Avatar answered Jan 02 '26 08:01

Hugeidea


In Rails, you have to do a total abstraction of your DB. Whatever you work with sqlite or mysql the steps are the same (except the first configuraion, but sqlite doesn't need).

The normal process is the following :

  1. Generate a Model with rails generator

    rails generate model Trade

  2. Edit the associated migration file, (something like 2012xxxxxxxx_create_trades.rb in db/migrate/) and put it the schema of Trade. syntax here

  3. Run rake db:migrate in order to apply the changes to the database

  4. Use your Model

like image 41
Thomas Guillory Avatar answered Jan 02 '26 10:01

Thomas Guillory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!