Hi All,
I''m trying to run a simple Activerecord query (I''m still
learning) but
I''m running into a brick wall on the following error:
[4;36;1mProduct Load (0.0ms) [0m [0;1mSELECT * FROM `products`
LIMIT 1 [0m
(eval):3:in `each_hash'': NULL pointer given (ArgumentError)
from (eval):3:in `all_hashes''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/mysql_adapter.rb:605:in
`select''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in
`select_all_without_query_cache''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in
`select_all''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:661:in
`find_by_sql''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1548:in
`find_every''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1505:in
`find_initial''
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:613:in
`find''
from C:/Documents and Settings/XYZ/My
Documents/NetBeansProjects/XYZ/vendor/plugins/testing.rb:32
The code that generates this error is as follows:
********************************************************************
require ''mysql''
require ''activerecord''
# enable activerecord logging
ActiveRecord::Base.logger = Logger.new(STDERR)
# ActiveRecord connection
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "pw",
:database => "etd"
)
# old-school mysql connection
my = Mysql::real_connect("127.0.0.1", "root",
"pw", "etd")
# create table and insert data
my.query "CREATE TABLE products (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id))"
my.query "INSERT INTO products
VALUES(''1'',''brick'')"
# ActiveRecord class
class Product < ActiveRecord::Base
end
# Activerecord query
product_row = Product.find(:first)
# Output result
puts product_row.name
*******************************************************************
Note that I''ve included the create and insert statements for the
convenience of anyone who may want to test this. Once the table and data
are there you can remove these statements and the error still occurs.
Other platform/version details as follows:
Box: Win32 (WinXP)
Activerecord ver: 2.3.4
IDE: Netbeans 6.7.1
Mysql gem ver: 2.8.1
Any help or suggestions on how to fix this would be greatly appreciated.
Regards,
Andre
--
Posted via http://www.ruby-forum.com/.