0

Suppose I have several different models and I'm building up a complex relation by passing them to other classes like:

class UserDefinedParameter
  def apply(base_relation)
    base_relation.where(:abc => self.value)
    #I want to see if base_relation was a ModelA here
  end
end

class UserDefinedSort
  def apply(base_relation)
    base_relation.sort("#{self.col} DESC")
  end
end

[ModelA, ModelB].each do |m|
  result = m.where(:user => current_user)
  UserDefinedParameter.all {|udp| result = udp.apply(result)}
  UserDefinedSort.all {|uds| result = uds.apply(result)}
end

What if I need to know whether I have ModelA or ModelB inside one of the apply methods? How could I determine this?

I can't do base_relation.first.class (like suggested here because the query isn't fully formed and ready to go to the database yet.

Community
  • 1
  • 1
Brian Glick
  • 2,201
  • 18
  • 20

1 Answers1

2

base_relation.klass should do the trick without a database query.

Dylan Markow
  • 123,080
  • 26
  • 284
  • 201