Question

I have a couple of scripts that interact with a MySQL database using ActiveRecord without the rest of the Rails package (although I have the complete Rails gem installed). However, trying to use these classes lately has been giving me the NoMethodError when I try to call find() on my ActiveRecord-descended class.
The class code is amazingly basic:

class LineItem <ActiveRecord::Base
   set_primary_key 'id'
   set_table_name  "line_items"
end

And yes, since I'm using a MySQL database I have a db connector file:

require 'rubygems'
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection(
  :adapter =>'mysql',
  :database=>'my_db',
  :username=>'my_user',
  :password=>'a_password',
  :host=>"ip_address",
  :port=>3306
)
ActiveRecord::Base.logger = Logger.new(STDERR) 

But when I run the script:

require "dbconn"
require "lineitem"

li = LineItem.new()
li.find(1)

I get the error:

NoMethodError: undefined method `find' for #<LineItem id: nil, trans_id: nil, trans_code: nil, data: nil>
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:260:in `method_missing'
from (irb):4

If anybody has any advice I would appreciate it. I'm using Ruby 1.8.7 on OSX 10.6.1 Yes, it worked fine on 10.5.x

Was it helpful?

Solution

Isn't find a class method instead of an instance method?

OTHER TIPS

You can use the find method only with data that is already in the database not with an empty ActiveRecord object like the one in your example.

It is quite normal for active_record objects not to have a find method (unless the corresponding table in the database has a column "find", but then that method would not take any arguments).

I think you want to do li = LineItem.find(1)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top