Alternative solution to session and global variables

Hello, guys.

I'm using session variables (session[:example]) in my app to do all the user session tasks. Additionally I'm also using them to save the data from a 3-page sata submission, so when when you click "Submit" on the last page, you can send the data from the all the pages. However, I have a problem: since I'm using session variables, when going to the first page having submitted the data to my database, that remains in the forms, as I let it keep them when submitting additional data that requires a submit and then back to the form (i.e.: A country's state, that triggers an action that allows you to select a city from that state).

I tried to use global variables from Ruby, but when another user is connected, if the submit data is not completed, he overwrites tthe data from the first user's previous pages and when the submission is done, the last form submits the data from both.

Resuming, what i'm looking for is a kind of variable that is global, but also individual at the same time for each user so it doesn't overwrites each other data. is there any kind of variable that let me do those things?

Greetings...

The Neurochild

Hello, guys.

I'm using session variables (session[:example]) in my app to do all the user session tasks. Additionally I'm also using them to save the data from a 3-page sata submission, so when when you click "Submit" on the last page, you can send the data from the all the pages. However, I have a problem: since I'm using session variables, when going to the first page having submitted the data to my database, that remains in the forms, as I let it keep them when submitting additional data that requires a submit and then back to the form (i.e.: A country's state, that triggers an action that allows you to select a city from that state).

I tried to use global variables from Ruby, but when another user is connected, if the submit data is not completed, he overwrites tthe data from the first user's previous pages and when the submission is done, the last form submits the data from both.

Resuming, what i'm looking for is a kind of variable that is global, but also individual at the same time for each user so it doesn't overwrites each other data. is there any kind of variable that let me do those things?

Nope, either the session or the database.

Fred

Oh! It's a shame. Sorry to hear that. I was hoping to use some variable that doesn't use a lot of space in the app. we have as much 50 session variables inside.

Too bad

Sounds like a refactoring opportunity :slight_smile:

I'm sorry but don't really understand. Are you saying that the users share the session information? How is that possible? I thought the session information is unique per user.

Pepe

Not at all, just thinking that 50 objects individually stored in session might be covered by a different design pattern :slight_smile:

I'm sorry Hassan, I didn't mean the question to be for you, questionning what you said but as a general question for everybody. I am not very experienced in developing web applications yet and my understanding is that the session information is generated for a given user and is not shared. I have a working application designed around that principle, which is actually identical to what the person in the first posting described. If my assumption about session data is not correct I need to know in order to re-engineer my app. ASAP. So please, set me straight if I'm wrong.

Thank you.

Pepe

The Neurochild wrote:

Hello, guys.

I'm using session variables (session[:example]) in my app to do all the user session tasks. Additionally I'm also using them to save the data from a 3-page sata submission, so when when you click "Submit" on the last page, you can send the data from the all the pages.

...

The Neurochild

NC, I've done a design like this before, and the first thing that comes to my mind is DON'T DO THAT. Trust me, it is a pain in the backside from beginning to end.

If you really must have a multi-page data form, I suggest (in hindsight) that you actually make each page of it supply data to a different table, and just associate these second and third, etc. tables with the first one.

If you have 3 pages worth of info, make three tables, "main", "second", and "third". Then set up model associations between them such that "main" has_one second and has_one third. Second "belongs_to" main, and "third" also belongs_to "main". (of course, you might choose different table names)

So, your first page might be a "user name" and basic "who are you" info. Maybe you call that table the "main" table. When they go to the second page, you save that first page of data to the "main" table, and keep the ID of that in a session variable. When they leave the second page (either forward or backward), you save the second page data to a new "second" table row, and associate it with the "main", by putting that id in second.main_id.

Similarly with the third page.

This will save you much angst in the long run. Cleaner code, fewer problems.

HTH, jp

That's true, but beside the point.

Storing 50 objects into the session is very different than storing, say, one ID that references a Model object with 50 attributes that's stored in the DB. Or 5 objects with 10 attributes, or whatever makes sense.

HTH,