# Find conditions question

**URL:** <https://www.ruby-forum.com/t/find-conditions-question/69430>\
**Category:** Rails\
**Created:** [September 20, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/find-conditions-question/69430 "2006-09-20T23:42:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 20, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/1 "2006-09-20T23:42:07Z")

</div>

Quick question , in a find statement, particularly the :conditions part.  
if you see something like conditions =\> [“user = ?”, user])  
what would you be drawing the first “user” from and the second ? I’m  
taking this from the AWDWR book.  
If user is a column / method in the model , are you just repeat it  
twice , or would you say  
user = ?, tom ? I’m confused.

TIA  
Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 20, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/2 "2006-09-20T23:58:10Z")

</div>

On 9/20/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Quick question , in a find statement, particularly the :conditions part.  
> if you see something like conditions =\> [“user = ?”, user])  
> what would you be drawing the first “user” from and the second ? I’m  
> taking this from the AWDWR book.  
> If user is a column / method in the model , are you just repeat it  
> twice , or would you say  
> user = ?, tom ? I’m confused.

The user in the quoted string is part of the final SQL statement; the  
other  
user is accessing a variable. The array method being used here provides  
reasonable security against SQL injection attacks - if you said  
“user = #{user}”, malicious users could potentially insert arbitrary  
code.  
The [] method automatically escapes SQL metacharacters, so it’s safe.

## Hope this helps!

Matt J.  
removed\_email\_address@domain.invalid  
President/Technical Director, Acme Art Company ([acmeartco.org](http://acmeartco.org))

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 12:08am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/3 "2006-09-21T00:08:10Z")

</div>

On 9/20/06, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > twice , or would you say  
> > –  
> > Matt J.

Let me make sure I got this right -  
if the column name was user and then I did  
user = “jack”  
:conditions =\> [“user ?” ← this is the column, user ← this is jack] ?

## Stuart

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 12:28am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/4 "2006-09-21T00:28:12Z")

</div>

On 9/20/06, Jon G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes. Be sure to check your development log, since it will show the  
> full SQL query that was actually run and can give great insight into how  
> what you’re passing in your find translates into the final query. I  
> recommend running a tail -f on the log in one window and experimenting  
> with different commands in script/console in another window side-by-side  
> for this purpose. Very educational.

Thank you Jon, excellent recommendation - didnt know about that fact.  
Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 12:18am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/5 "2006-09-21T00:18:11Z")

</div>

Dark A. wrote:

> Let me make sure I got this right -  
> if the column name was user and then I did  
> user = “jack”  
> :conditions =\> [“user ?” \<- this is the column, user \<- this is jack] ?
> 
> Stuart

Yes. Be sure to check your development log, since it will show the  
full SQL query that was actually run and can give great insight into how  
what you’re passing in your find translates into the final query. I  
recommend running a tail -f on the log in one window and experimenting  
with different commands in script/console in another window side-by-side  
for this purpose. Very educational.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 1:12am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/6 "2006-09-21T01:12:14Z")

</div>

The

Parameters: {“action”=\>“index”, “controller”=\>“cdetails”}

part suggests to me you did something from the browser window, not the  
console. When you load a page in your rails app in a browser rails is  
going to make LOTS of SQL queries. Your syntax looks correct though.  
Assuming that all the model relationships and tables are setup  
correctly, from this…

@cdetails = Cdetail.find(:all, :conditions =\> [“user\_id = ?”, user])

I would expect to see something like…

SELECT \* FROM cdetails WHERE user\_id = 11

In the log, which I assume is what you’re going for.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 1:28am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/7 "2006-09-21T01:28:15Z")

</div>

> > user = 11  
> > @cdetails = Cdetail.find(:all, :conditions =\> [“user\_id = ?”, user])

If you just did that from the console, Rails would run just the one  
query on  
the Cdetails table. If you’re running it from the browser, there is  
probably  
some code referencing the user object(not just code that says user = 1)  
in  
there that you’re missing.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 12:40am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/8 "2006-09-21T00:40:13Z")

</div>

On 9/20/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Yes. Be sure to check your development log, since it will show the  
> > full SQL query that was actually run and can give great insight into how  
> > what you’re passing in your find translates into the final query. I  
> > recommend running a tail -f on the log in one window and experimenting  
> > with different commands in script/console in another window side-by-side  
> > for this purpose. Very educational.

Actually , I’m totally confused about what I’m seeing in the log . It  
looks like there are multiple queries going on. The one that stands  
out is the 2nd one , but I’m putting the find on the cdetails table  
not the users.

user = 11  
@cdetails = Cdetail.find(:all, :conditions =\> [“user\_id = ?”, user])

Parameters: {“action”=\>“index”, “controller”=\>“cdetails”}  
[4;36;1mUser Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM userse[0m  
[4;35;1mUser Load (0.000000)e[0m e[0mSELECT \* FROM users WHERE  
(users.`id` = 11 ) LIMIT 1e[0m  
[4;36;1mCdetail Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM  
cdetailse[0m  
e[4;35;1mSQL (0.000000)e[0m e[0mSELECT count(\*) AS count\_all FROM  
cdetails e[0m  
e[4;36;1mCdetail Load (0.000000)e[0m e[0;1mSELECT \* FROM cdetails  
ORDER BY id LIMIT 0, 5e[0m

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 21, 2006, 2:54am UTC](https://www.ruby-forum.com/t/find-conditions-question/69430/9 "2006-09-21T02:54:21Z")

</div>

On 9/20/06, Jon G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @cdetails = Cdetail.find(:all, :conditions =\> [“user\_id = ?”, user])
> 
> I would expect to see something like…
> 
> SELECT \* FROM cdetails WHERE user\_id = 11
> 
> In the log, which I assume is what you’re going for.

Yep, works correctly in console. I guess something maybe is  
interferring in the console.

Stuart
