[英]Rails ActiveRecord - How do I determine the owner in a polymorphic relation?
[英]How to determine type of ActiveRecord::Relation
我有這種方法
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
如何確定get_list()
中coll
的類型?
我嘗試了coll.instance_of?(Unit)
和coll.is_a?(Unit)
以及self.instance_of?(Unit)
和self.is_a?(Unit)
但它們都返回false
。
您可以執行以下操作
coll.klass == Unit
#OR
coll.method == Unit
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.