Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing model count in view (MVC)

I would like to put some statistics right in front of my site, like:

# jobs/index.html.haml
.panel
    .panel-heading
        %h4 Statistics
    .panel-body
        .col-md-9.col-xs-9
            %h5.pull-left Users
        .col-md-3.col-xs-3
            %h5.pull-right= @usercount

        .col-md-9.col-xs-9
            %h5.pull-left Companies
        .col-md-3.col-xs-3
            %h5.pull-right= @companycount

        .col-md-9.col-xs-9
            %h5.pull-left Categories
        .col-md-3.col-xs-3
            %h5.pull-right= @categorycount

        .col-md-9.col-xs-9
            %h5.pull-left Total offers gathered
        .col-md-3.col-xs-3
            %h5.pull-right= @jobcount

At the moment these variables are all set in controller:

# controller: jobs, action: index
@jobcount = Job.count
@usercount = User.count
@categorycount = Category.count
@companycount = Job.distinct.count('company')

but I believe that's the worst way to do this in MVC. Can anybody tell me how it should be done?

like image 786
mtszkw Avatar asked Oct 30 '22 21:10

mtszkw


1 Answers

I would go with this style: @statistics = { job_count: Job.count, user_count: User.count, category_count: Category.count, company_count: Job.distinct.count('company') }

like image 60
Vibol Avatar answered Nov 15 '22 07:11

Vibol