I heart Ruby and Rails 6

Posted by dburkes Monday, February 04, 2008 21:36:00

Tonight I needed to find out if there were any rooms on Lingr that hadn't had a room image created for them. This should never happen, but some exception emails that were being generated indicated that it was happening.

In about 10 seconds, I whipped this up in the console:

Room.find(:all, :select => :id).collect(&:id).inject([]) {|r, i| 
    r << i if !Image.find_by_imageable_type_and_imageable_id('Room', i)
    r
}

Awesome

Comments

Leave a response

  1. SamFebruary 04, 2008 @ 11:05 PM
    I can guess what is happening there but I don't think I fully understand it. Would you care to elaborate what the .collect(....).inject(...) in combination with the block do?
  2. Ikai LanFebruary 05, 2008 @ 07:10 AM
    I <3 ruby />d get beat up.
  3. ujihisaFebruary 05, 2008 @ 09:04 AM
    Room.find(:all, :select => :id).map(&:id).inject([]) {|r, i| Image.find_by_imageable_type_and_imageable_id('Room', i) ? r + i : r }
  4. ujihisaFebruary 05, 2008 @ 07:08 PM
    s/ i / [i] /g
  5. Chris BooneFebruary 05, 2008 @ 08:46 PM
    Room.find(:all, :select => :id).collect(&:id).reject do |room|
      Image.find_by_imageable_type_and_imageable_id('Room', room)
    end
    
  6. ynwApril 20, 2008 @ 09:13 AM
    yes yes yes...
Comment