On Apr 27, 11:58 pm, Phrogz <[email protected]> wrote:
> I'm making a web app with Sinatra and Sequel. My web app may have many
> simultaneous users modifying the DB at once. Each user has a unique id
> associated. One of my models records changes in a separate log table,
> associated with the user who made the change. For example:
>
>   class Bug < Sequel::Model
>     def tag_names=( desired_tags )
>       # add/remove tags from a join table as necessary
>       BugLogEntry << {
>         :created_on => Time.now,
>         :bug_id     => id,
>         :user_id    => active_user_id,
>         :field_id   => BugLogField::TAG_NAMES,
>         :new_value  => tag_change_summary
>       }
>     end
>     def after_create
>       BUG_FIELDS.each do |field|
>         BugLogEntry << {
>           :created_on => Time.now,
>           :bug_id     => id,
>           :user_id    => active_user_id,
>           :field_id   => BugLogField.const_get(field.upcase),
>           :new_value  => self.send(field).to_s
>         }
>       end
>     end
>     def before_save
>       if old_bug = Bug[id]
>         BUG_FIELDS.each do |field|
>           new_value = self.send( field )
>           old_value = old_bug.send( field )
>           if new_value != old_value
>             BugLogEntry << {
>               :created_on => Time.now,
>               :bug_id     => id,
>               :user_id    => active_user_id,
>               :field_id   => BugLogField.const_get(field.upcase),
>               :new_value  => new_value,
>               :old_value  => old_value
>             }
>           end
>         end
>       end
>     end

You should be calling super if you override the hooks, and you may
want to use before_update instead of before_save.

> My question is: how should I record or access the active_user_id in a
> thread-safe manner? My only guess thus far has been something like
> this:
>
>   class Bug < Sequel::Model
>     attr_accessor :active_user_id
>   end
>   ...
>   # Record who touched this instance before making any changes
>   my_bug.active_user_id = session[:user_id]
>   my_bug.tag_names = "foo bar"
>   my_bug.name = "Updated for hotness"
>   my_bug.save
>
> My question (finally) is: will the above work? Does each user always
> get a new instance of a model (even for the same record)? And to be
> sure, both after_create and before_save have full access to instance
> variables on the model, right? (Nothing has been cleared and reloaded?)

The above should work and is a good way to do it.  The more magical
approach would be to set a thread local variable in a controller
before filter, but I would recommend against that approach.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to