Mongoid finders not working?

后端 未结 3 1381
無奈伤痛
無奈伤痛 2021-01-31 03:31

I have set up a rails3+mongoid application and when I open the rails console, none of the finders seem to work - http://d.pr/FNzC

User.all
User.find(:all, :condi         


        
3条回答
  •  借酒劲吻你
    2021-01-31 04:01

    Okay, so this is part of what makes mongoid irritating for newcomers. People expect methods like User.all to actually return an array when it really just returns the Criteria object.

    In order to provide the syntatic sugar of chainable methods and other fancy query mechanisms, Mongoid seems to use a lazy loading type thing.

    You can do:

    #array index
    User.all[0]
    
    #first/last
    User.all.first
    
    #each over things, print out all the users
    User.all.each {|u| p u}
    
    #edit, I forgot to include this, which is probably what you really want
    #this spits out an array
    User.all.to_a
    

    It makes it difficult to quickly verify that things are working for newcomers from ActiveRecord where User.all just returns an array.

提交回复
热议问题