Injecting SQL conditions to models?

Hi all,

I have an app where I have Users, users upload images, post blog
comments, etc. All has been fine until we now need to disable a user.
Which means all calls to User.find_by_id() now need to exclude any
disabled users. Also searches for such things as photos and blog
comments need to exlude those belonging to disabled users.

Aside from going through the whole application and addition conditions
to calls such as find_by_id (and other such searches), is there some
clean way that I could add this requirement? I simply need to exclude
user, comment and photo searches of disabled users.

Hope this makes sense.

Any help would be appreciated.

Thanks,
Diego

Hi Diego, you can do the following:

a) add a field to the user table called ‘disabled’ and this field is
set to ‘false’ by default.

b) change how you perform the find

 user = User.find( :first, :conditions => [ "disabled = ? and id =

?", “false”, input_id ] )

c) update your admin area so that you can simply disable a user

Good luck,

-Conrad

Thanks Conrad. Appreciate the response.

Hi there :wink:

Another tips that could be usefull : use the “conditions” in the linked
tables.

For example, after adding the disabled field in the user table, if you
have a file table with a link “has_many” you can write :

class File < ActiveRecord::Base
has_many :users,
:conditions => ‘disabled = false’
end

Then you will only retrieve files with users that complet the condition
‘disabled = false’. And you will not have to rewrite the User.find() in
each linked table.
You can then access all the disabled users via the User class, but not
via the linked tables.

Tony

I’m doing something similar, except I’ve taken it a step further. Rather
than having a boolean field, I have an integer field (technically a
foreign
key, although the meanings of the numbers are hard-coded as well) which
means I can have different levels of blocking. 0 means “active”, 1 means
“temporarily blocked” (still receives email alerts and stuff, but
temporarily can’t log in), 2 means “frozen” (still appears as a member
in
the system, but doesn’t receive emails and can’t log-in), and 3 means
deleted (completely hidden from everyone, but still on the system to
maintain database integrity, and in case deletion was in error).

Hope this helps,
-N