1

I have a Rails application where I use some global variables to show some results on a page based on dates. The date range variables are global. Now the problem is when the application is accessed by more than one person simultaneously, the global variables are shared and and not unique for each instance of the application.

Can someone point me to a good direction of handling variables for each instance of the application.

1 Answer 1

3

Instead of globals (which you should literally never use), you should use instance variables. Here they are set for finding orders in a date range

class OrdersController < ActionController::Base
  def index
    @start_date = params[:start_date] || 30.days.ago
    @end_date   = params[:end_date] || 30.days.ago
    @orders =  Order.where("start_date >= ?", @start_date).where("end_date <= ?", @end_date)
  end
end

note: literally every example you'll find on the web will use the above style and won't use ruby global variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.