4

最初我以数据类型Hash导入数据,因为我有一个名为 schedule 的列,我需要我的数据库中特定列“schedule”的类型。我尝试过的代码是

       schedule = scheduleWorld.all
       schedule.each do |sec|          
         sec.attributes.each do |key, value, type|
         puts "%%%%%%%%%%%%%%%%%%%%%%%%%"  
         puts  key
         puts  value
         puts  type
         puts "%%%%%%%%%%%%%%%%%%%%%%%%%"   
         end
       end  

这样我的类型就为零了,我尝试了另一个代码是

  schedule = scheduleWorld.where({schedule:{$type=>2}})

错误是

undefined method `specify' for nil:NilClass
(eval):2:in `where'

有人知道吗?

4

1 回答 1

0

类型只是 mongodb 文档的属性哈希中的一个键值对。所以你总是可以这样获取它。我正在使用 Mongoid。确切的名称可能因您的 ORM 而异。

       schedule = scheduleWorld.all
       schedule.each do |sec|
         type = sec.attributes["_type"]          
         sec.attributes.each do |key, value|
         puts "%%%%%%%%%%%%%%%%%%%%%%%%%"  
         puts  key
         puts  value
         puts "%%%%%%%%%%%%%%%%%%%%%%%%%"   
         end
       end 
于 2013-06-04T05:03:58.043 回答