a method to search for empty stuff

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

I'd try...

def self.find_empty_things(name_of_attrib)
  Resort.find(:all, :conditions => ["#{name_of_attrib} IS NULL"])
end

HTH,
Bill

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

bill walton 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,

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

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