我有以下疑问,我知道它看起来很糟糕。
注意:我将它们添加到实例变量中,因为我将在highcharts js库中使用它们,因此每个实例变量将代表highcharts js中的单个列/ bar /行
tickets_controller.rb
def priorities_chart
priority_base = Ticket.includes(:priority)
@high_tickets_counter = priority_base.where(priorities: {name: 'High'}).count
@medium_tickets_counter = priority_base.where(priorities: {name: 'Medium'}).count
@low_tickets_counter = priority_base.where(priorities: {name: 'Low'}).count
end
def statuses_chart
status_base = Ticket.includes({ticket_statuses: :status})
@open_tickets_counter = status_base.where(statuses: {ticket_status: 'Open'}).count
@in_progress_tickets_counter = status_base.where(statuses: {ticket_status: 'In Progress'}).count
@pending_tickets_counter = status_base.where(statuses: {ticket_status: 'Pending'}).count
@solved_tickets_counter = status_base.where(statuses: {ticket_status: 'Solved'}).count
@closed_tickets_counter = status_base.where(statuses: {ticket_status: 'Closed'}).count
end
def subjects_chart
subject_base = Ticket.includes({student: :subjects})
@subject_1_counter = subject_base.where(subjects: {subject_title: 'Subject 1'}).count
@subject_2_counter = subject_base.where(subjects: {subject_title: 'Subject 2'}).count
@subject_3_counter = subject_base.where(subjects: {subject_title: 'Subject 3'}).count
@subject_4_counter = subject_base.where(subjects: {subject_title: 'Subject 4'}).count
@subject_5_counter = subject_base.where(subjects: {subject_title: 'Subject 5'}).count
end
我需要改善这种难看的外观并实现以下目标:
1-如果我有另外一个不同名字或超过10个科目的科目,我需要一个查询来处理这个问题而不指定我在这里做的主题。
2-是否有更好的方法来编写priority_chart和statuses_chart?
答案 0 :(得分:0)
我试图将我的工作示例翻译成你的案例。
def subjects_chart
search = [ 'Subject 1', 'Subject 2', 'Subject 3', 'Subject 4' ]
subject_base = Ticket.includes({student: :subjects})
@subject_counter =
subject_base.where( subjects: { subject_title: search } ).
group( 'tickets.id' ).
count( 'subjects.id' )
端
这有用吗?