Find(:all) problem

Newbie question, trying to get data from a mysql database. I can write
to it fine, but when I try to retrieve it I get the following error
message:

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/attribute_methods.rb:260:in
method_missing': undefined methodfind’ for #<Stuff id: nil, mp1: nil,
mp2: nil> (NoMethodError)

    from C:/rubyapps/stuff/lib/get.rb:16:in `m'

    from C:/rubyapps/stuff/lib/get.rb:10:in `initialize'

    from C:/rubyapps/stuff/lib/get.rb:22:in `new'

    from C:/rubyapps/stuff/lib/get.rb:22

This is the code below.

Thanks for your help

require ‘environment’
require ‘rubygems’
require ‘Stuff’

class Get

def initialize

m

end

def m
@mystuff =Stuff.new

b = @mystuff.find( :all, :select => ‘mp1’)
puts “mp1 has the following values: #{b}”

end
end

Get.new

Hi

You don’t do find on an instance of the model, you do it on the model
itself.
So your find should be

Stuff.find( :all, :select => ‘mp1’)

Simon

On Wed, 08 Jul 2009 07:00:47 +0800, Mark P.

Simon M. wrote:

Hi

You don’t do find on an instance of the model, you do it on the model
itself.
So your find should be

Stuff.find( :all, :select => ‘mp1’)

Simon

On Wed, 08 Jul 2009 07:00:47 +0800, Mark P.

Thanks! worked great

find is a class method, not an instance method.

Change
b = @mystuff.find(:all, :select => ‘mp1’)
to
b = Stuff.find(:all, :select => ‘mp1’)

Note: ‘b’ will be an array of ActiveRecords with only the mp1 attribute
set.
If you want an array of strings, add:

b = b.map{|rec| rec.mp1}

HTH,
Jeffrey

Quoting Mark P. [email protected]:
[snip]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs