Reputation: 12650
I'm learning rails and I'm just trying to get the basics down.
I just installed devise and created a home controller. If I want to get the number of users (just return the number of records in the db), what should I do from the home controller? It should interact with a model, right? Can the home
controller interact with the users
model and the home
model? Or is that bad practice?
I know I could do a simple google search for this, but I have a lot of questions and Stackoverflow is legit. :)
Take it a step further?
How would I get a 'live feed' so that the count changes on the home page when a record is added?
Upvotes: 2
Views: 2664
Reputation: 38711
Something like
User.all.length
User.count
should work, where the first one is highly inefficient and the second one much better Rails style.
It's not necessarily a bad idea to directly access that. Why not?
Upvotes: 0
Reputation: 222248
You can get a count of the users using User.count
.
In your template, you can add something like
There are <%= User.count %> users.
Upvotes: 7