如何确定ActiveRecord :: Relation的类型

时间:2017-05-22 08:30:41

标签: ruby-on-rails activerecord

我有这个方法

def get_list(limit = nil, page = nil)
  coll = all
  coll = (block_given?) ? (yield coll) : coll
  rowcnt = coll.count
  pages = (limit && limit > 0) ? (rowcnt.to_f/limit).ceil : 1
  coll = coll.limit(limit) if limit && limit > 0
  coll = coll.offset((page-1)*limit) if limit && limit > 0 && page && page > 1
  ret = { rows: coll.all.to_a, rowcnt: rowcnt, page: page || 1, pages: pages, limit: limit || 0 }
  def ret.serialize(include = nil)
    self[:rows].collect! do |row|
      hash = include ? (row.serializable_hash(include: include.incllist)) : row.serializable_hash
      (hash['code'] && hash['code'].end_with?('(TR)')) ? nil : ((block_given?) ? yield(hash,row) : hash)

    end
    self[:rows].compact!
    return self.rest_success,true,self
  end
  return ret
end

我打电话给

Unit.get_list() do |q|
# some logic
end

如何确定coll中的get_list()类型?

我尝试使用coll.instance_of?(Unit)coll.is_a?(Unit)以及self.instance_of?(Unit)self.is_a?(Unit),但所有人都返回false

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作,

coll.klass == Unit
#OR
coll.method == Unit