# Injecting SQL conditions to models?

**URL:** <https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977>\
**Category:** Rails\
**Created:** [February 27, 2007, 8:28am UTC](https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977 "2007-02-27T08:28:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 27, 2007, 8:28am UTC](https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977/1 "2007-02-27T08:28:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 27, 2007, 9:08am UTC](https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977/2 "2007-02-27T09:08:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 28, 2007, 12:53am UTC](https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977/3 "2007-02-28T00:53:55Z")

</div>

Thanks Conrad. Appreciate the response.

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [March 1, 2007, 11:22am UTC](https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977/4 "2007-03-01T11:22:42Z")

</div>

Hi there 😉

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

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [March 1, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/injecting-sql-conditions-to-models/86977/5 "2007-03-01T13:27:19Z")

</div>

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
