Sunday, October 31, 2010

Rails Application variable

It almost seemed that there was no easy way out of putting application scope variables in the database.
But there is a way out:
    if Thread.main[:uuid] == nil
      Thread.main[:uuid]=0
    end
    uid = Thread.main[:uuid] += 1


This code accesses the mail thread of the rails process and puts a variable on it. (the thread is an object and can have key => value sets)
And since rails has only one main thread... its shared with all the threads underneath.

Good Luck

2 comments:

  1. This is a pretty nice find -- thanks for sharing! It would be nice if there was an approach for persistent, application-level objects that was endorsed by the Rails team.

    ReplyDelete
  2. Thanks Justin
    Anyway, I would rather have a better solution then to write on the main thread, endorsed by the rails people.

    ReplyDelete

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.