Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In home_Controller code change #39

Open
deependersingla opened this issue Oct 10, 2013 · 1 comment
Open

In home_Controller code change #39

deependersingla opened this issue Oct 10, 2013 · 1 comment

Comments

@deependersingla
Copy link

In home_controller.rb
index method

@user=User.all.to_a from @user=User.all (According to mongoid documentation) , otherwise user will get error that user is a nil class.

@BandanaPandey
Copy link

BandanaPandey commented Aug 29, 2016

Hi, you are right, but you have misunderstood something here.
@users is Mongoid::Criteria .
Criteria is like Relation object in ActiveRecord.
In case of Mongoid, the Mongoid::Criteria module is the core object for querying MongoDB. Every time a query is fired, a criteria is created. Once you have a criteria, you can get the elements, executing an actual query to the database, with a method that is supposed to iterate over the elements or return one element, for eg: .to_a, .each, .map, etc.

So here on @users we have already used @users.each in views/home/index.html.erb to fetch the elements from the database.

As a result there is no need to use @users = User.all.to_a , as we are using @users.each, so no error occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants