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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, > BillBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.