How do I work with a rails 4 collection and determine an object count by enum? -
How do I work with a rails 4 collection and determine an object count by enum? -
i have rails 4 app, user owns sales_opportunity, , each sales_opportunity can have "pipeline_status" implemented enum. works fine, trying create user display shows each of these pipeline_status(es) in graphical manner (a series of chevrons lead "prospect" "closed_won") , want display badge on each chevron shows number of opportunities user has within each pipeline_status.
i trying utilize count method in rails, , pass in conditions - whilst tells me how many sales_opportunities user has (in total), not take status argument , filter count depending on enum pass. can help or point me toward resource can help me larn right way utilize please?
sales_opportunity.rb:
class salesopportunity < activerecord::base default_scope { order('close_date asc') } belongs_to :user belongs_to :company has_many :timeline_events, dependent: :destroy has_many :swots, dependent: :destroy validates :close_date, presence: true validates :user_id, presence: true validates :company_id, presence: true validates :opportunity_name, presence: true enum pipeline_status: [ :prospect, :qualifying, :demonstrating, :negotiating, :closed_won, :closed_lost, :dormant ] end
my current attempts show these on "user dashboard":
<div class="container-fluid"> <div class="row"> <div class="col-lg-12"> <div id="chevrons"> <ul> <li><a href="#1">prospect <span class="badge"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 0") %></span> </a></li> <li><a href="#2">qualifying <span class="badge"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 1") %></span> </a></li> <li><a href="#3">demonstrating <span class="badge"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 2") %></span> </a></li> <li><a href="#4">negotiating <span class="badge"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 3") %></span> </a></li> <li><a href="#5">closed <span class="badge", id="won"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 4") %></span> <span class="badge", id="lost"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 5") %></span> </a></li> <li id="dormant"><a href="#1">dormant <span class="badge"><%= @user.sales_opportunities.count(:conditions => ":pipeline_status == 6") %></span> </a></li> </ul> </div> </div>
edit: clarity current results of page show each badge number one. have given user 1 sales_opportunity (enum: "prospect"), , should register "1" "prospect" chevron badge , "0" other chevron badges work expect.
i wondering whether might need extract logic separate method phone call within either sales_opportunity code or perhaps within helper method, figured there must way working based on standard rails count feature. help gratefully appreciated.
count
no longer accepts conditions argument. utilize where
instead. example:
replace:
@user.sales_opportunities.count(:conditions => ":pipeline_status == 1")
with:
@user.sales_opportunities.where(pipeline_status: 1).count
ruby-on-rails ruby-on-rails-4 enums
Comments
Post a Comment