[Using XP, MySQL 5.0, Ruby 1.8.4]
Hello, this is my first post.
I am new to Ruby, coming from about 6 years of ''import
java.util.*''s. I
am using ActiveRecord, both on Rails and outside Rails.
On Rails I have no trouble. Outside is where it gets confusing.
I have created three tables (so far) and their three corresponding
classes. Since I''m having the same trouble on all classes,
I''ll just
use one as an example. This example doesn''t have an auto-incrementing
id column (I won''t add any new records), but the problem is just the
same as those in tables without auto-incrementing id.
On MySQL, the nodes table is defined as such:
CREATE TABLE `nodes` (
`id` int(5) NOT NULL COMMENT ''This column correspond to the node
id'',
`node_name` varchar(64) NOT NULL default ''N/A'' COMMENT
''This column
holds the node name'',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
And it''s class is:
require ''rubygems''
require ''active_record''
class Node < ActiveRecord::Base
def to_s
"#{id}) #{node_name}"
end
end
###########
I insert 17 records here.
Well. After establishing a connection I do the following:
# # # # # # #
all_nodes = Node.find(:all)
all_nodes.each do |n|
puts "#{n}" # this is the "problem line"
end
# # # # # # #
But when i run it I get this:
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/mysql_adapter.rb:292:in
`columns'': undefined method `each'' for
#<Mysql:0x2cf2270>
(NoMethodError)
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:696:in
`columns''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:704:in
`columns_hash''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1562:in
`column_for_attribute''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1362:in
`id''
[the stack continues downward to my own *.rb file]
Even if I change the "problem line" to something like
puts "#{n.id} with name #{n.node_name}" # problem line 2
I get the same error! Thing is, if I substitute "problem line 2" with
say just an output message, it gets displayed 17 times, one per
record... so I KNOW the data is being loaded.
One [extremely ugly and wrongheaded IMHO] approach has been adding
accessing methods to the Node class:
def id
@attributes[''id'']
end
def node_name
@attributes[''node_name'']
end
And it works but for setters I have no luck and besides, this isn''t the
way it''s supposed to be.
Now, I''ve looked around and all examples work perfectly by calling out
the column name on the object (i.e. as I did on "problem line 2") but
here I get this error! And I know this might be a case of a simple
omission or a RTFM thing but so far I haven''t had much luck. Any
suggestions?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---