Ways to make variables accessible in mulitple controllers?

I have an array of user IDs that I need to access in several places (multiple controllers, not only the UsersController). The actual user IDs in the array are selected dynamically by the admin of the system. That is, the admin is selecting a subset of users, and then I need to be able to work with that subset in other areas of the program.

Here are options I’ve considered so far for handling this (with the drawback in parathesis):

  1. store as session variable (cookies)
  2. use Flash (not meant to store anything other than error/message text)
  3. create a model for dynamic variables, store/delete them in the database as needed (feels hacky, and difficult if multiple admins on system simultaneously)
  4. add a boolean column to the User model for when admin selects user (won’t
    work with multiple admins accessing system simultaneously)
  5. from the original controller, after a subset of users is chosen then
    "redirect_to" AnotherController#create method that accepts/expects
    POST method (hacky, and complicates the create method)
  6. use global variables (don’t think this will work with multiple admins)

And perhaps there are other ways. My goal was to avoid storing anything client side and to avoid hacks (I want to learn the rails way). I do not think instance variables will work because they are bound to a particular class, and the variables I need must be available in different classes (different controllers).

Does anyone have advice / best practices for handing this situation in rails?

I’m not sure what a best practice would be, but at first glance I think storing the list in the session or the database would both be good options. The session should be fairly easy to do and reason about, the database would be more robust and probably better if your concerned with preventing two admins from selecting/working with the same list of users at once.

I had a similar situation once and decided ultimately to let each controller involved accept the list as a direct parameter and passed it between them by putting it in a hidden input or url parameters as needed. This keeps everything stateless which is nice, but I can think of a number of scenarios where the approach would break down, so it might not be useful to you.


EDIT: I may have misunderstood the requirements here. Feel free to ignore me if my response doesn’t make much sense.

Why won’t this work? Would the admins differ in who they want selected for this flag? If so, how likely is this scenario. It strikes me as the easiest thing to do and would allow you to get all the information you need by checking current_user to see if that flag is set.

Thanks @pat and @derekprior for responding! The system will have multiple admins with multiple roles, so I can’t (unfortunately) get away with having a boolean column for the User model (one admin could overwrite another’s selection if both are logged in).

After researching it more I think the best thing will be a session variable. It turns out the default for a session variable is a cookie, but the session data can be stored in the database if desired.

Also this was helpful to find out: instance variables like @current_user are only set for the duration of the request. This means using a redirect_to won’t work, which is what I was trying to do (redirect from one controller to another and somehow “pass” the selected data). Even if I managed to pass it once (e.g via the flash method), the data would be lost if the admin went to any other page (e.g. hit the back button).

So it looks like the best path forward in my case is a session variable stored in the database. Thanks again for taking the time to give your thoughts, which caused me to dig deeper.

@cs7 I want to question the feature you’re implementing. Why are the admins selecting users, and how often will they select them? Can you change the feature to make implementation easier, or change the implementation to accomplish the same goal?

For example, will the admin select this same group of users often? If so, save them to a DB. Do these users share a common property - they all signed up yesterday, or they all post over 100 messages per day? If so, pass something about that property. Does the admin do the same thing to the same types of users every time? Make that easier.

Hope that helps.

Hi @mikeburns thanks for taking the time to comment. The selections are dynamic (i.e. selected students will be different each time). In terms of frequency of use, the admins will select dynamic groups every time they log in (core requirement). Check out activeadmin.info for screenshots of the “filter” feature. That’s what is being used to cull students by various attributes and then select certain students. The problem is I need the selected group (an array of student primary ids) to be available outside ActiveAdmin in other controllers for the duration of the session. I think the solution comes down to using session variables stored in the session table of the DB (instead of as cookies on the client). So the data is being stored in the DB, just in the session table. Let me know if you see a better way though (which is why I posted this to the forum).