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 method `find'' 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
--
Posted via http://www.ruby-forum.com/.
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 Preston <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 method `find'' 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
Simon Macneall 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 PrestonThanks! worked great -- Posted via http://www.ruby-forum.com/.
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 Preston
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:
[snip]> def m
> @mystuff =Stuff.new
>
> b = @mystuff.find( :all, :select => ''mp1'')
> puts "mp1 has the following values: #{b}"
>
> end