Ruby on Rails - global variable persistence across requests - ruby-on-rails

In a RoR application, I would like to load a set of key/values from db into a global variable which persists across requests and clear the variable as and when I need. My application runs in a WAS cluster. I tried a few things. 1. Used memcachestore and read/write/clear as needed. 2. Load the key/values in a yaml file and clear it when required. Please let me know if I could do it differently.Thanks.

Don't we have flash and session hashes which can store any object we need?

Related

where to store variable on RoR on heroku that needs to change several times a day?

Do I use an Heroku environment variable? can I change the value from within Rails?
I used a file (File.read and File.write) but Heroku's ephemeral system does not save the new value stored in the file.
If you need to use global settings variable, you can use this https://github.com/ledermann/rails-settings. We used this in some projects
Use Postgres then. Make a simple model to store your settings.
Loading a Postgres record takes, typically, less than a millisecond. The cost of loading this once on Rails startup is inconsequential.

Use two neo4j databases in single rails app

I have two neo4j databases running on two different hosts. I connected my rails app to one of them while generating the app. Now I want to use other database as well with the app. How can I configure the app to connect to both the databases?
There’s not currently a good way to configure one Ruby process to use two sessions at the same time. If you are using Rails you can change the server by setting the NEO4J_URL environment variable. Otherwise you’d need to manage the session by setting Neo4j::ActiveBase.current_session or Neo4j::ActiveBase.on_establish_session (which will set the session for each new thread, which may be needed if you are running a multi-threaded process)
See: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb
As Brian mentioned currently we cannot configure one Ruby process to use two sessions at the same time. We have to manage the session by setting Neo4j::ActiveBase.current_session (See: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb)
The neo4j.yml sets the Neo4j::ActiveBase.current_session for you in the railtie. If you set Neo4j::ActiveBase.current_session after the app has started up it will override what was in the neo4j.yml. The current_session needs to be a Neo4j::Core::CypherSession object from the neo4j-core gem. (See the readme: https://github.com/neo4jrb/neo4j-core)
Also keep in mind, that currently neo4j does not support having different session for each model. So you might experience problem if, setting the session inside model. A better way would be to set session in the normal runtime of the app. You also might want to wrap the Neo4j::Core::CypherSession to get Query Proxy instead of Neo4j::Core objects. To this you have to specify wrap_level: :proc while declaring the adaptor. (Refer: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/session_manager.rb#L14)
So in all, here is what you need to do
http_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new('http://neo4j:7474',{wrap_level: :proc})
Neo4j::ActiveBase.current_session = Neo4j::Core::CypherSession.new(http_adaptor)
this will establish a wrapped session with the desired database in 'http://neo4j:7474'

Why should we avoid using class variables ## in rails?

Why should we avoid using class variables ## in rails? Is there any security loopholes with that. Please answer as I am new with rails. and I am much using instance variable #variable_name . I tried once ##variable_name .
I know only about class variable is, Class variable is sharable between object
But I really would like to know Why should we avoid using class variables ## in rails?
Simply because they are not thread safe. Many rails=capable servers are multi-threaded. That means there may be multiple running instances of your application at any given time and any request by one of your users is going to be arbitrarily assigned to one of them. Class variables are not shared between processes so there is a possibility that your class variable will be different in a subsequent request.
Even if you deliberately manage to run your app in a single threaded server, there is no guarantee that your app won't be restarted between requests, losing your class variable.
If you want functionality similar to what class variables provide, I strongly recommend that you look into key-value stores such as Memcached or Redis.

Ruby on Rails - Global Variable?

I am a new Ruby on Rails user and had a question. I have an idea of what I want my Users DB to look like but was wondering whether or not I should add an additional value to it. Basically I need a variable to signal to all users that it is safe to proceed with a certain action. This variable would be persistent across all users and should be visible to all users, but I want the server to be able to change this variable as well. When programming in other languages, I would use a global variables, so I wanted to check if that is also the case here. If so, would this be the best approach for going about it: Site-Wide Global Variables in Ruby on Rails. Also, how would I update the global variables. Thanks for any help!
A global variable doesn't fit your need. It doesn't spread across all the Ruby processes. If your web server spawns 5 ruby processes to handle 5 request at the same time, the variable defined in the first process won't be visible to the others.
There are other solutions available. You can use a database and store the flag/information on the database. Otherwise, you can use a file and store the value in the file.
The best solution would be an in-memory shared data source, such as memcached or Redis.

Rails: How can you access session variables using multiple controllers?

I am having a problem with using session variables. I have two controllers named 'graduate_students_controller' and 'current_students_controller'. Each of these controllers control different view files. I am using session variables with both these controllers to store session information.
Here's the problem. Let's say I have two view files 'reports/current_students_list', 'reports/graduate_students_list' each controlled separately by the above mentioned controllers.
Now if I try to open those two web pages from within the same browser and try to work with them simultaneously, I get 'nil object access' error from the firstly loaded page. The 'nil object' refers to a session variable that the first page is supposed to access. However, when I use any of those two web applications individually, they work fine.
So its seems to me that session variables of the firstly loaded web app. are getting overwritten by the secondly loaded web app. maybe because the second page stores a new cookie over the first one?
How do I fix this?
Any suggestion is much appreciated.
To clarify a bit more: The two controllers belong to the same Rails application. And I am not using identical session variable names within both controllers. So I cannot see why they can get overwritten
I am new to rails and I would really appreciate some help with this problem. Thanks.
I'm not sure if you are running two apps, or are referring to two controllers under the same app. If you are looking at different web apps, then I think you are using the same name and session key in your environment for each of these apps. Try changing the key value in your environment.rb:
config.action_controller.session = { :key => "_myapp_session", :secret => "..." }
If you are using the same session variable from two different controllers in the same application, then you'll need to write your code to accomodate this, though I wouldn't recommend doing this. When accessing your session data, check for nil values:
session[:some_key].nil?
and make sure that common code (i.e. in the application_controller.rb) isn't overwriting your values.

Resources