SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    boston
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .collect but no records

    This piece of code is from flickr.rb which searches flickr. What I'm having a problem with is when no photos are returned based on the keywords I'm providing, it throws an error. I think its because it's trying to .collect when there are no items to collect. I tried checking for nil and zero but I couldn't get either to work. Can anybody help? And I even tried 'rescue nil', what does that do exactly? Thanks for any help!

    Code:
    Code:
      # Implements flickr.photos.getRecent and flickr.photos.search
      def photos(*criteria)
        photos = (criteria[0]) ? photos_search(criteria[0]) : photos_getRecent
        photos['photos']['photo'].collect { |photo| Photo.new(photo['id'])  }
      end
    Error:
    can't convert String into Integer

    trace:
    #{RAILS_ROOT}/app/controllers/flickr.rb:103:in `[]'
    #{RAILS_ROOT}/app/controllers/flickr.rb:103:in `photos'
    #{RAILS_ROOT}/app/controllers/flickr.rb:103:in `each'
    #{RAILS_ROOT}/app/controllers/flickr.rb:103:in `collect'
    #{RAILS_ROOT}/app/controllers/flickr.rb:103:in `photos'
    #{RAILS_ROOT}/app/controllers/business_controller.rb:24:in `show'
    --------------
    Thanks for any help! (And how do you post ruby code, whats the vb code...???)

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    USA
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well 'rescue nil' will assign a variable to nil if the first condition fails, so you will still get a nil object, probably not what you want.
    I think you may want to edit the assignment line, check if it is nil or whatever value it is set to when no photos are returned and have a special return case for that, so when the function returns to your app you know it return nothing (i.e. I'd have it return nil or false) and then do nothing if the method returns nil.
    (I haven't used this flickr service so I can't be more specific.) good luck!

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    boston
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, I actually found the solution here:
    http://blog.tourb.us/archives/flickr...cture-perfect/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •