# Creating a generic function for objects of different classes

How might I refactor this to get rid of all the if-else?

  def self.dispatches_for(object)
scope = Dispatch.asc(:days)
if object.class == Habit
scope.where(:habit=>object).map{|d|d}
elsif object.class == User
scope.where(:coach=>object).map{|d|d}
elsif object.class == Content
scope.where(:content=>object).map{|d|d}
end
end


def self.dispatches_for(object)

• Also, if you're using ActiveRecord or similar you can do scope.where(klass=>object).to_a instead of map{|d|d}. – kardeiz Sep 3 '13 at 16:47