Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: Storing Session in Active Record (not cookie)
    primarykey
    data
    text
    <p><em>I trolled google university for about an hour or so but there is no single good documentation on this topic :-( Hoping someone can help. I am willing to buy a book as well as long as someone can tell me which one.</em></p> <p>I am using following versions:</p> <ul> <li>rails 3.2.6, </li> <li>ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]</li> </ul> <p>To start using session storage in Active Record instead of cookie, I did following:</p> <ol> <li><p>Update <code>config/initializers/session_store.rb</code>. <em>Commented first line and uncommented the last one, so I have:</em></p> <pre><code># Be sure to restart your server when you modify this file. # Myapp::Application.config.session_store :cookie_store, key: '_elegato_session' # Use the database for sessions instead of the cookie-based default, # which shouldn't be used to store highly confidential information # (create the session table with "rails generate session_migration") Myapp::Application.config.session_store :active_record_store </code></pre></li> <li><p><code>rake db:sessions:create</code></p> <pre><code>invoke active_record create db/migrate/20120729025112_add_sessions_table.rb </code></pre></li> <li><p><code>rake db:migrate</code></p> <pre><code>== CreateLinkedinUsers: migrating ============================================ -- create_table(:linkedin_users) -&gt; 0.0236s == CreateLinkedinUsers: migrated (0.0237s) =================================== == AddSessionsTable: migrating =============================================== -- create_table(:sessions) -&gt; 0.0012s -- add_index(:sessions, :session_id) -&gt; 0.0006s -- add_index(:sessions, :updated_at) -&gt; 0.0006s == AddSessionsTable: migrated (0.0026s) ====================================== </code></pre></li> <li><p>To find out what table is actually created, I open the sqlite3 file </p> <pre><code>sqlite&gt; .schema sessions CREATE TABLE "sessions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "session_id" varchar(255) NOT NULL, "data" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL); CREATE INDEX "index_sessions_on_session_id" ON "sessions" ("session_id"); CREATE INDEX "index_sessions_on_updated_at" ON "sessions" ("updated_at"); </code></pre></li> </ol> <p>So, there is a "data" field in sessions table that will store everything related to the session.</p> <h2>Now, onto my question :-)</h2> <p>I need to store values for 2 variables in session: access_token and request_token. Can I use following? (the way I used to store session values for a cookie)</p> <pre><code>session[:access_token] = &lt;blah&gt; session[:request_token] = &lt;some other blah? </code></pre> <p>And if it does work, does ruby store both variables in the "data" field of the table as an array. </p> <p>Thanks much for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload