Where do I do this?

Hey guys,

I’m working on a small custom admin panel for my own personal website. I am working on the part to allow me to manage my Flickr photographs

I have a Flickr class which contains methods to get the list of sets from Flickr, the photos in a particular set and another to grab the sizes for a particular photo.

What I want to do is grab a list of all the sets - (which I can get via a method in my Flickr class) but then for each set that is returned, look through my local Albums (what I call sets on my site) DB table to see if that sets id is in my table and then put a tick or cross next to each set name in the admin panel highlighting if I have that set on my site or not.

So, I know what I wish to do but I am just not 100% sure where to put this logic of looping through the set list returned from Flickr.

Is anyone able to help guide me here?

Kind regards,
Neil

Hi Neil,

You’ll want to get the collection in your controller, loop through in the view and put a tick or cross based on one of the properties on the model.

Nothing wrong with simple logic like this in your views

if @set_ids.include?(album.set_id)

Hey Mark. Thanks for the reply. The problem is that for every set returned from Flickr I need to do a find on my local Albums table to see if a corresponding record exists. So I basically need to do that check and I want to build up a sort of hash with the album name, flickr id and a tick or cross whether I have a local copy or not. I’m thinking the logic would be something along the lines of:

@sets = Flickr.get_set_list

@sets.each_with_index do |index, set|
Album.find_by_flickr_id(set[id])
end

But I need to put that info into a hash and then pass that to the view but is this too much logic to put in my controller? and if so, to me it doesn;t seem right putting it in the model, so I’m not really sure where to put it.

Also, how would I get that data into the hash I could do with ending up with?

Thanks,
Neil

I’m not sure what’s inside the get_set_list method but it sounds like you should just be able to do one query with a join between them.

If that’s not the case you might just be able to search for the albums with the id’s from the first query.


@set_ids = Flickr.get_set_list.map{ |set| set.id }
@albums = Album.where("flickr_id in (?)", set_ids)

But I need to put that info into a hash and then pass that to the view but is this too much logic to put in my controller? and if so, to me it doesn;t seem right putting it in the model, so I’m not really sure where to put it.

Yes, you want to keep your controllers are light as possible. The model is the correct place to put this sort of thing, or if you’re doing a lot with Flickr then you might put it all inside the Flickr module.

class Album
  def self.within_flickr_sets
    set_ids = Flickr.get_set_list().map{ |set| set.id }
    Album.where("flickr_id in (?)", set_ids)
  end
end

Then in your controller you can just have this:

@albums = Album.within_flickr_sets()