Search code examples
ruby-on-railsrubycountscaffold

Ruby on Rails - Count Entries on Scaffold


In a simple Ruby on Rails applicaiton, I have a Scaffold called "Proposals" on which one of its columns is a string called "creationyear". I'm trying to count how many proposals have been created each year when the index page is loaded doing the following (in "proposals_controller.rb" file):

def index
  @proposals = Proposal.all
  @count2015 = Proposal.count(:all, :conditions => "creationyear = 2015")
  @count2016 = Proposal.count(:all, :conditions => "creationyear = 2016")
end

Somehow it is not working, since it gives me back on both variables the total number of entries on the scaffold.

Does anyone knows how to solve this one? I'm using Rails v4.2.6.

Thanks!


Solution

  • @count2015 = Proposal.where(creationyear: '2015').count
    

    If creationyear is a string, otherwise without the quotes.