Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 548365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:16+00:00 2026-05-13T11:04:16+00:00

I’m having a tough time deciding how to refactor this method in my controller.

  • 0

I’m having a tough time deciding how to refactor this method in my controller. The idea is that (in this case) it graphs the users that joined (or were created) in the past two weeks.

You might be wondering why I did the @graph_limit thing, and that is because I always want the day that has the most results to be the tallest bar on my bar chart (which in the view are just created with css by making the height of the <div> using css).

Basically I want to dry it up and… ya know just about improve this method as much as possible:

# Controller
def index

  two_weeks_ago = Date.today - 13.days

  @users_graphed = User.count(:conditions=>["created_at >= ?", two_weeks_ago], :order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])

  two_weeks_ago.upto(Date.today) do |day|
    @graph_limit = 100/@users_graphed.values.max.to_f
    @users_graphed[day.to_s] ||= 0
  end

end

Also I should mention, that you guys are probably going to rip my code to shreds… so I’m bracing for the outcome.

# View
<% @users_graphed.sort.reverse.each do |user| %>
  <li>       
    <% content_tag :div, :style => "height: #{number_with_precision(user[1] * @graph_limit, :precision => 2)}px; ", :class => "stat_bar" do %>
      <%= content_tag(:span, user[1]) unless user[1] == 0 %>
    <% end %>
  </li>
<% end %>

Ultimately and what my real goal here is to put this into my application controller and be able to chart any models by it’s create_at times. maybe something like tasks.chart_by(2.weeks). How would you guys get this separated out into something I can use throughout the whole app?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:04:17+00:00Added an answer on May 13, 2026 at 11:04 am

    I agree with Joseph that your controller here is doing a lot of work that should be done in the model. Any time you’re specifying multiple find parameters in your controller, ask yourself whether or not that should be in your model instead.

    You’re doing a lot of iterating here that seems needless. Firstly, You shouldn’t be calculating @graph_limit inside the loop. You’re recalculating it 14 times, but the value is going to be the same every time. Do that outside the loop.

    Secondly, that sort.reverse in your view sticks out. You’re already sorting in your finder (:order => 'DATE(created_at) DESC'), and then you’re sorting again in your view and then reversing it? You should instead be asking the database for the values in the final order you want them. Then to make your zero-filling code work you can just reverse it, doing Date.today.downto(two_weeks_ago) instead of upto.

    I would say that you should really be doing this all in SQL, but unfortunately (as perhaps you’ve discovered) MySQL makes it difficult to fill in missing days without creating a calendar table to join against.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's a simple example using the XML features in SQL… May 15, 2026 at 11:57 am
  • Editorial Team
    Editorial Team added an answer I usually add a term that indicates for what it… May 15, 2026 at 11:57 am
  • Editorial Team
    Editorial Team added an answer Yes. They are the same. They all generate the RFC… May 15, 2026 at 11:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.