ActiveRecord::StatementInvalid could not find table


#1

Hi,

Have searched and searched the group and the net with no joy on
this…

I have set up users table and am trying to insert newly registered
users with the following code from:

class UserController < ApplicationController

def index
end

def register
@title = “Register”
if request.post?
#raise params[:user].inspect
@user = User.new(params[:user])
if @user.save
render :text => “User created!”
end
end
end
end

I receive the following error:

ActiveRecord::StatementInvalid in UserController#register

Could not find table ‘users’
RAILS_ROOT: /Users/Will/Development/rails_space

Application Trace | Framework Trace | Full Trace
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/sqlite3_adapter.rb:29:in table_structure' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ core_ext/object/misc.rb:39:inreturning’
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/sqlite3_adapter.rb:28:in table_structure' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/ connection_adapters/sqlite_adapter.rb:213:incolumns’
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:1276:in columns' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:3008:inattributes_from_column_definition_without_lock’
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
locking/optimistic.rb:66:in attributes_from_column_definition' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2435:ininitialize’
/Users/Will/Development/rails_space/app/controllers/user_controller.rb:
10:in new' /Users/Will/Development/rails_space/app/controllers/user_controller.rb: 10:inregister’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:1322:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:1322:inperform_action_without_filters’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:617:in call_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ filters.rb:610:inperform_action_without_benchmark’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ core_ext/benchmark.rb:17:inms’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:10:in realtime' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ core_ext/benchmark.rb:17:inms’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ rescue.rb:160:inperform_action_without_flash’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
flash.rb:141:in perform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:523:insend’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in process_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ filters.rb:606:inprocess’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:391:in process' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:386:incall’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
routing/route_set.rb:433:in `call’

It looks as if Rails is trying to find that table using the sqlite3
adapter, bt my db is configured to use mysql. Here is database.yml:

MySQL. Versions 4.1 and 5.0 are recommended.

Install the MySQL driver:

gem install mysql

On Mac OS X:

sudo gem install mysql – --with-mysql-dir=/usr/local/mysql

On Mac OS X Leopard:

sudo env ARCHFLAGS="-arch i386" gem install mysql – --with-mysql-

config=/usr/local/mysql/bin/mysql_config

This sets the ARCHFLAGS environment variable to your native

architecture

On Windows:

gem install mysql

Choose the win32 build.

Install MySQL and put its /bin directory on your path.

And be sure to use new-style password hashing:

http://dev.mysql.com/doc/refman/5.0/en/old-client.html

development:
adapter: mysql
encoding: utf8
reconnect: false
database: rails_space_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock

Warning: The database defined as “test” will be erased and

re-generated from your development database when you run “rake”.

Do not set this db to the same as development or production.

test:
adapter: mysql
encoding: utf8
reconnect: false
database: rails_space_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock

production:
adapter: mysql
encoding: utf8
reconnect: false
database: rails_space_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock

I have verified existence of the table and using the Ruby console can
add records no problem.

Here is the user.rb model file:
class User < ActiveRecord::Base

Max & min lengths for all fields

SCREEN_NAME_MIN_LENGTH = 4
SCREEN_NAME_MAX_LENGTH = 20
PASSWORD_MIN_LENGTH = 4
PASSWORD_MAX_LENGTH = 40
EMAIL_MAX_LENGTH = 50
SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGTH…SCREEN_NAME_MAX_LENGTH
PASSWORD_RANGE = PASSWORD_MIN_LENGTH…PASSWORD_MAX_LENGTH

Text box sizes for display in the views

SCREEN_NAME_SIZE = 20
PASSWORD_SIZE = 10
EMAIL_SIZE = 30

validates_uniqueness_of :screen_name, :email
validates_length_of :screen_name, :within => SCREEN_NAME_RANGE
validates_length_of :password, :within => PASSWORD_RANGE
validates_length_of :email, :maximum => EMAIL_MAX_LENGTH

validates_format_of :screen_name,
:with => /^[A-Z0-9_]*$/i,
:message => "must contain only letters, " +
“numbers, and underscores”
validates_format_of :email,
:with => /^[A-Z0-9._%-]+@([A-Z0-9-]+.)+[A-Z]
{2,4}$/i,
:message => “must be a valid email address”

end

Here is my environment.rb file:

Be sure to restart your server when you modify this file

Specifies gem version of Rails to use when vendor/rails is not

present
RAILS_GEM_VERSION = ‘2.3.2’ unless defined? RAILS_GEM_VERSION

Bootstrap the Rails environment, frameworks, and default

configuration
require File.join(File.dirname(FILE), ‘boot’)

Rails::Initializer.run do |config|

Settings in config/environments/* take precedence over those

specified here.

Application configuration should go into files in config/

initializers

– all .rb files in that directory are automatically loaded.

Add additional load paths for your own custom dirs

config.load_paths += %W( #{RAILS_ROOT}/extras )

Specify gems that this application depends on and have them

installed with rake gems:install

config.gem “bj”

config.gem “hpricot”, :version => ‘0.6’, :source => "http://

code.whytheluckystiff.net"

config.gem “sqlite3-ruby”, :lib => “sqlite3”

config.gem “aws-s3”, :lib => “aws/s3”

Only load the plugins named here, in the order given (default is

alphabetical).

:all can be used as a placeholder for all plugins not explicitly

named

config.plugins =

[ :exception_notification, :ssl_requirement, :all ]

Skip frameworks you’re not going to use. To use Rails without a

database,

you must remove the Active Record framework.

config.frameworks -=

[ :active_record, :active_resource, :action_mailer ]

Activate observers that should always be running

config.active_record.observers

= :cacher, :garbage_collector, :forum_observer

Set Time.zone default to the specified zone and make Active Record

auto-convert to this zone.

Run “rake -D time” for a list of tasks for finding time zone

names.
config.time_zone = ‘UTC’

The default locale is :en and all translations from config/locales/

*.rb,yml are auto loaded.

config.i18n.load_path += Dir[Rails.root.join(‘my’, ‘locales’, '*.

{rb,yml}’)]

config.i18n.default_locale = :de

end

I made sure to configure the app with the -d mysql option when
creating it.

Any help hugely appreciated.

thanks


#2

I’m not sure how it’s possible to have a different database adapter
called up in the script/console from the one you get through script/
server since both paths use the same database.yml file. One way to
confirm what you are running in each case is:

Both methods start with you in your project directory. Do not change
any files between runs.


  1. For console case type: script/about

You should see something like:

About your application’s environment
Ruby version 1.9.1 (powerpc-darwin9.6.0)
RubyGems version 1.3.1
Rack version 1.0 bundled
Rails version 2.3.2
Active Record version 2.3.2
Action Pack version 2.3.2
Active Resource version 2.3.2
Action Mailer version 2.3.2
Active Support version 2.3.2
Application root /Users/rick/test
Environment development
Database adapter mysql
Database schema version 20090401180059


  1. For browser case type: script/server
  2. Then, in browser window display site: localhost:3000/rails/info/
    properties

You should see:

Ruby version 1.9.1 (powerpc-darwin9.6.0)
RubyGems version 1.3.1
Rack version 1.0 bundled
Rails version 2.3.2
Active Record version 2.3.2
Action Pack version 2.3.2
Active Resource version 2.3.2
Action Mailer version 2.3.2
Active Support version 2.3.2
Application root /Users/rick/test
Environment development
Database adapter mysql
Database schema version 20090401180059

What you see in both tests should be identical. Obviously, for you
the important fields are Database adapter and Database schema version.

Rick