# A method to search for empty stuff

**URL:** <https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550>\
**Category:** Rails\
**Created:** [April 25, 2010, 4:49pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550 "2010-04-25T16:49:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 25, 2010, 4:49pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550/1 "2010-04-25T16:49:06Z")

</div>

I’m trying to write some tools to help me clean up my data, first stop  
is trying to find empty attributes in my Resort model…the following  
works,

def self.find\_empty\_resort\_heights  
resorts = []  
Resort.all.each do |resort|  
if resort.TopLiftHeight.nil?  
puts resort.name + “has no height entered”  
end  
end  
end

I’m not using the array in this example, but it gives an idea. I’d like  
to rewrite this so that I pass in an attribute name and it gives me back  
the resorts where that attribute name is nil or whatever. I can’t seem  
to do it, I thought it’d start like this…

def self.find\_empty\_things(name\_of\_attrib)

code to do it

How do I write that first line so I can pass in an attribute?

bb

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 25, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550/2 "2010-04-25T16:58:40Z")

</div>

On Sun, 2010-04-25 at 16:49 +0200, bingo bob wrote:

I’d try…

def self.find\_empty\_things(name\_of\_attrib)  
Resort.find(:all, :conditions =\> ["#{name\_of\_attrib} IS NULL"])  
end

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 25, 2010, 5:04pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550/3 "2010-04-25T17:04:41Z")

</div>

Thanks Bill,

Thats looks like it’d work, in case it helps any, I ended up with…

def self.find\_nil(name)  
resorts = Resort.find(:all, :conditions =\> {name =\> nil})  
resorts.each do |resort|  
puts resort.name  
end  
puts resorts.length  
end

which seems good.

Anyone with any tips on how i might help myself with tasks to clean a  
large database would be interesting to hear!

bb

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 25, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550/4 "2010-04-25T17:06:21Z")

</div>

bill walton wrote:

> On Sun, 2010-04-25 at 16:49 +0200, bingo bob wrote:
> 
> I’d try…
> 
> def self.find\_empty\_things(name\_of\_attrib)  
> Resort.find(:all, :conditions =\> [“#{name\_of\_attrib} IS NULL”])

Right. The OP’s original approach involved fetching all the records  
from the DB, them doing the query in the app layer. That’s silly and  
inefficient.

> end
> 
> HTH,  
> Bill

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 25, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550/5 "2010-04-25T18:05:01Z")

</div>

i’ve now added validation failure messages like this…

def self.find\_nil\_and\_save(name)  
resorts = Resort.find(:all, :conditions =\> {name =\> nil})  
resorts.each do |resort|  
puts resort.name + " #{name} is empty. Enter a value…"  
new\_value = gets.chomp  
puts resort.name + " Thanks, you entered #{new\_value}"  
resort.update\_attributes(name =\> new\_value)  
# puts resort.name + " updated… #{name} is now  
#{resort.{name}.to\_s}"  
if resort.save  
puts resort.name + " updated…"  
else  
puts resort.name + " not updated…"  
resort.errors.each { |attr,msg| puts “#{attr} - #{msg}” }  
end  
end  
end

which is nice… but i’d like to refactor

if validation fails it should allow the user to reenter details for that  
resort

also, the whole thing should be wrapped in an “exit” option, at the  
moment i do ctrl+c ! to break out of it.

how do i do that?

bb

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 25, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/a-method-to-search-for-empty-stuff/187550/6 "2010-04-25T17:48:14Z")

</div>

following on from this…

I’m trying to do this now…

def self.find\_nil\_and\_save(name)  
resorts = Resort.find(:all, :conditions =\> {name =\> nil})  
resorts.each do |resort|  
puts resort.name + " #{name} is empty. Enter a value…"  
new\_value = gets.chomp  
puts resort.name + " Thanks, you entered #{new\_value}"  
resort.update\_attributes(name =\> new\_value)  
# puts resort.name + " updated… #{name} is now  
#{resort.{name}.to\_s}"  
puts resort.name + " updated…"  
end  
end

that works but I can’t figure this line out

# puts resort.name + " updated… #{name} is now #{resort.{name}.to\_s}"

How do i simply output the updated attribute value on that line, I  
appreciate what I have there is wrong!

bb
