Forum: Ruby on Rails Injecting SQL conditions to models?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
05bf52a3e873f6847b05536a99bec4ad?d=identicon&s=25 Diego (Guest)
on 2007-02-27 08:28
(Received via mailing list)
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.

40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-02-27 09:08
(Received via mailing list)
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,

05bf52a3e873f6847b05536a99bec4ad?d=identicon&s=25 Diego (Guest)
on 2007-02-28 00:53
(Received via mailing list)
Thanks Conrad. Appreciate the response.
99dfa3f52932cfa4dab2a7ae6dd7ef39?d=identicon&s=25 Tony (Guest)
on 2007-03-01 11:22
Hi there ;)

Another tips that could be usefull : use the "conditions" in the linked

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'

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.

D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2007-03-01 13:27
(Received via mailing list)
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
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
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,
This topic is locked and can not be replied to.