Forum: Ruby on Rails Alternative solution to session and global variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ed3050ec0a96c03ef1304e7ad1ff46fb?d=identicon&s=25 The Neurochild (Guest)
on 2008-12-16 15:49
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-16 16:36
(Received via mailing list)
On 16 Dec 2008, at 14:49, The Neurochild wrote:

> requires a submit and then back to the form (i.e.: A country's state,
> overwrites each other data. is there any kind of variable that let me
> do those things?
>
Nope, either the session or the database.

Fred
Ed3050ec0a96c03ef1304e7ad1ff46fb?d=identicon&s=25 The Neurochild (Guest)
on 2008-12-16 16:52
(Received via mailing list)
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
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2008-12-16 17:42
(Received via mailing list)
On Tue, Dec 16, 2008 at 7:51 AM, The Neurochild <neurochild@gmail.com>
wrote:
>
> ... we have as much 50 session variables inside.

Sounds like a refactoring opportunity :-)

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
10f7e00c1c5c60e2173995eb16f4ffc7?d=identicon&s=25 pepe (Guest)
on 2008-12-18 04:54
(Received via mailing list)
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

On Dec 16, 11:41 am, "Hassan Schroeder" <hassan.schroe...@gmail.com>
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2008-12-18 06:48
(Received via mailing list)
On Wed, Dec 17, 2008 at 7:54 PM, pepe <Pepe@betterrpg.com> wrote:
>
> I'm sorry but don't really understand. Are you saying that the users
> share the session information?

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

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
10f7e00c1c5c60e2173995eb16f4ffc7?d=identicon&s=25 pepe (Guest)
on 2008-12-19 05:05
(Received via mailing list)
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

On Dec 18, 12:47 am, "Hassan Schroeder" <hassan.schroe...@gmail.com>
8dad1ec4d769734583f45fbbee5cd009?d=identicon&s=25 Jeff Pritchard (jeffpritchard)
on 2008-12-19 06:02
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
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2008-12-19 19:12
(Received via mailing list)
On Thu, Dec 18, 2008 at 8:05 PM, pepe <Pepe@betterrpg.com> wrote:

> understanding is that the session information is generated for a given
> user and is not shared.

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,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
This topic is locked and can not be replied to.