# Question on clearing Active Record objects

**URL:** <https://www.ruby-forum.com/t/question-on-clearing-active-record-objects/165238>\
**Category:** Rails\
**Created:** [April 10, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/question-on-clearing-active-record-objects/165238 "2009-04-10T16:43:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mifi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@mifi](https://www.ruby-forum.com/u/mifi)\
**Post date:** [April 10, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/question-on-clearing-active-record-objects/165238/1 "2009-04-10T16:43:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mifi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@mifi](https://www.ruby-forum.com/u/mifi)\
**Post date:** [April 10, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/question-on-clearing-active-record-objects/165238/2 "2009-04-10T16:59:27Z")

</div>

On Apr 10, 3:43 pm, Michael Furmaniuk \<rails-mailing-l…@andreas-  
[s.net](http://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

---

<div class="post-metadata">

**Author:** ![mifi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@mifi](https://www.ruby-forum.com/u/mifi)\
**Post date:** [April 10, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/question-on-clearing-active-record-objects/165238/3 "2009-04-10T17:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mifi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@mifi](https://www.ruby-forum.com/u/mifi)\
**Post date:** [April 10, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/question-on-clearing-active-record-objects/165238/4 "2009-04-10T19:29:58Z")

</div>

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
