views:

348

answers:

1

I am trying to store data from the current_user in a User model with attr_accessor :offset. I would like to fetch records of a different model using the offset stored in the current_user model. When I change this offset from a different model it does not save it?

I feel a database Column for the offest is overkill. This offset would be 0..n integer.

+1  A: 

Assuming I understood your question correctly:

A database column is really the only way for data to persist in a model beyond a reload. The current_user variable gets around this by storing it in the sessions for your controllers and views. Regardless of where you're trying to use current_user, I have the feeling your problem stems from the method that current_user is passed from one request to the next, any changes you make to current_user will not carry over to the next request.

The current_user id is stored in the session hash at login. The first time you call current_user as part of a controller action the authenticated_system module finds the User based on the id in the session hash. Meaning any changes you make to current_user are lost unless you save it before the controller action finishes. A database column is the only way to do that.

However you can ignore the current user entirely and add offset to the session hash, with session[:offset] = offset. Refer to it in your controllers/views the same way. So long as your user doesn't end their browsing session session[:offset] will return the offset value you're trying to keep.

But, if offset is going to be a user preference that should persist between login sessions, then it really does belong in your user model as a database column.

EmFi
Thank you for your reply. This offset have to be set to 0 when new session is made so session would be good enough to store this offset. This offset is passed to navigation of pictures on a wall where user can navigate on x and y axis. I made desicion to persist data to db and it seems to work. Hope this won't cause big problems when there will be lots of more users?!
ttomppa