multi user CRUD concurrency

If your asking if another user modifies an active_record that you have stored in a users session. Don’t do that.

The general consensus is don’t store them there. Rails sessions make no attempt to keep in sync with the database, it just serializes the object into the session, it doesn’t hit the db again. The way that a lot of people look after this is to store just the id into the session, then have a lazy loading method to get the record.

As an example, the restful_authentication plugin tracks the current user with these two methods

# Accesses the current user from the session.
def current_user
  @current_user ||= (session[:user] && User.find_by_id(session[:user])) || :false
end

# Store the given user in the session.
def current_user=(new_user)
  session[:user] = (new_user.nil? || new_user.is_a?(Symbol)) ? nil : new_user.id

  @current_user = new_user
end

There have been many blogs and threads on this list dealing with this issue. A quick google should turn up some answers.

HTH
Daniel

Dave Rose wrote:

Daniel ----- wrote:

is there any tutorial/comments/blog about Rails multi-user concurrency
and how to prevent other users info from creeping into your session
if you both are accessing the same record...a rails newbie asks???
--

If your asking if another user modifies an active_record that you have
stored in a users session. Don't do that.
The general consensus is don't store them there. Rails sessions make no
attempt to keep in sync with the database, it just serializes the object
into the session, it doesn't hit the db again. The way that a lot of people
look after this is to store just the id into the session, then have a lazy
loading method to get the record.
As an example, the restful_authentication plugin tracks the current user
with these two methods
# Accesses the current user from the session.
def current_user
@current_user ||= (session[:user] && User.find_by_id(session[:user]))
>> :false
end
# Store the given user in the session.
def current_user=(new_user)
session[:user] = (new_user.nil? || new_user.is_a?(Symbol)) ? nil :
new_user.id
@current_user = new_user
end
There have been many blogs and threads on this list dealing with this
issue. A quick google should turn up some answers.
HTH
Daniel
...just using Rails simple defaults.... and storing things into a database how does rails protect the database records ovriting newly changed things that another user just changed...i'm going to re-write a multi-user business process tracking system and want to know will my data be safe from one user who makes changes to ID=100 at the same time another user 100 miles away also to ID=100..does rails protect each user from the other? i'll be using Oracle

Check out optimistic locking.