Question on clearing Active Record objects


#1

I have the following function that will do a query for me, the problem I
am having (being new to Ruby and RoR) is how to reinitialize the object.
Right now it always returns the same value.

class AutoHarnesses < ActiveRecord::Base
end

def get_testruns(io)
    agents = AutoHarnesses.new()
    io.puts "What Agent's test runs do you want to see?\n"
    io.print "Agent Name = "
    my_agent = io.gets.chomp
    io.puts "Looking for #{my_agent}\n"
    begin
        # I think this is the query I want....
        agents = AutoHarnesses.first()
        io.puts %{Found #{agents.testname} - #{agents.status}}
    rescue
        puts $!
    end
end

I thought by having a new call at the beginning I could reinitialize the
object and clear it out so that I would always get a new one, or if
there was no result it would be empty when if did the Found statement.
Is there a way to assure that I am clearing out any earlier results from
the get_testruns function?

Thanks!


#2

On Apr 10, 3:43 pm, Michael Furmaniuk <rails-mailing-l…@andreas-
s.net> wrote:

    io.print "Agent Name = "

I thought by having a new call at the beginning I could reinitialize the
object and clear it out so that I would always get a new one, or if

Well you do create a new instance of AutoHarnesses, but you then
overwrite that with the result of AutoHarnesses.first which will
always return the same thing.

Frd


#3

Frederick C. wrote:

Well you do create a new instance of AutoHarnesses, but you then
overwrite that with the result of AutoHarnesses.first which will
always return the same thing.

Frd

Ok…I had forgotten about first being like that. So if I have to look
for just one machine what is the best option for select in this case?
I’m just trying to keep it simple and I haven’t had much luck with using
some of the select options as I keep getting undefined method errors.


#4

Sometimes a beer at lunch can help…still getting used to ActiveRecord
and its SQL format but this ended up working for me:

def get_testruns(io)
    agents = AutoHarnesses.new()
    # This gets the particular agent to search for
    io.puts "What Agents test runs do you want to see?\n"
    io.print "Agent Name = "
    my_agent = io.gets.chomp
    begin
        # Look for agent by machine, get only the latest result
        agents = AutoHarnesses.find_by_machinename(my_agent)
        if agents.nil?
            # The agent is not in the database
            io.puts "We did not find any records matching your 

request.\n"
else
io.puts %{Found #{agents.testname} - #{agents.status}}
end
rescue
# Print out errors in case of problems
puts $!
end
end