views:

20

answers:

1

Hi,

I'm having trouble deleting from active record store.

I want to delete based on the session data:

ActiveRecord::SessionStore::Session.find(:all).each do |s|
  if s.data[:userid] == 1234
    s.destroy
  end
end

does not seem to work, but:

ActiveRecord::SessionStore::Session.delete_all(["updated_at < ?", 12.hours.ago])

seems to work and:

ActiveRecord::SessionStore::Session.find(:all).each do |s|
  if s.data[:userid] == 1234
    ActiveRecord::SessionStore::Session.delete_all("session_id = ?", s.session_id)
  end
end

also doesnt work.

I'm running version rails 2.3.2, ruby 1.8.7.

+1  A: 

You can just pass Active Record's delete method the ID of the object you want to delete:

ActiveRecord::SessionStore::Session.delete(1234)

On another note, sure where the .data attribute is meant to be coming from in the above example. Session.find(:all) will return an array of Session objects which you then iterate through. Session does not have a userid column unless you are hacking the default Session store to add it.

Toby Hede
every user that logs on gets a userid. If they are not logging in, then this userid is nil so I don't need to worry about it. Thanks.
Andy