2

My database contains data for multiple years (each table has a year column). Whenever users login they have to select a year to work with.

After login, all data accessed by the user is scoped to the selected year. The selected year is stored in the session variable and in the global variable.

I am using active_admin and active_admin_import, and I want to limit importing data only to the selected year.

I do this by adding a validation in my model. First, I tried to use the session variable but I realised that the model doesn't have access to the session; therefore, I ended up putting the selected year into a $year variable.

This was working in development, but after I pushed to the production the global variable is blank within the model, and my validation doesn't work.

From what I read I understand it is a bad idea to make session data accessible to the model. I can't think about a different solution. Any suggestions?

Additional info: As the main table can store multiple years, I import into a working table (which has the validation rule for one year), then copy data to the production table.

This way, if the import data has the wrong year or multiple years, the import fails and user gets an error message.

1 Answer 1

3

As you rightly say your session data isn't available in your model, just your controller (and maybe views - not sure).

Therefore the way around this is to call your model method from the controller, where the data is available, with your session data as an argument. Let's say you've got a method that sets one of the model's attributes to true or false depending on the session data:

apps/models/model.rb

def thing_to_do(session_data)
  # do something with your session data
end

Then in your controller action that you're currently hitting, for example the show action:

app/controllers/random_controller.rb

def show
  model.thing_to_do(session[:user_id])
end

This will call the model method 'thing_to_do' with the session variable as an argument. Obviously you can include as many session variables as needed

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

4 Comments

Mark, I'm not sure how to do this. The validation happens when I click Import button on the screen generated by active_admin_import gem. I assume it's either called by the gem or by Active Record during the import. I don't know how to call the model's validation method explicitly.
Can you show the method that you need to get session variables into please?
This is validation rule in my model: validates_inclusion_of :dm_fy, :in => [$year.to_i], message: "is invalid. Only data for fiscal year #{$year} can be imported. Invalid fiscal year"
Sorry, I think I was not completely clear. I don't have a method that I am calling. Instead I have a validation rule in my model that checks that one of the columns can have only particular value. So in the code in my previous comment global variable $year could be a session variable session[:year]. So I don't explicitly call validation method and as the session is not available in my model the validation fails because $year is blank.

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.