# SQL WHERE equivalent in rails

**URL:** <https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797>\
**Category:** Rails\
**Created:** [April 26, 2006, 12:49am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797 "2006-04-26T00:49:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 12:49am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/1 "2006-04-26T00:49:03Z")

</div>

What is the equivalent of the WHERE SQL condition in rails?

I’m thinkin this could be something like:

\<% for product in @products where category=“1” %\>

but obviously the syntax is wrong.

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 1:03am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/2 "2006-04-26T01:03:16Z")

</div>

BB wrote:

> What is the equivalent of the WHERE SQL condition in rails?
> 
> I’m thinkin this could be something like:
> 
> \<% for product in @products where category=“1” %\>
> 
> but obviously the syntax is wrong.

Try something like this:

@products = Product.find(:all, :condition =\> “category = ‘1’”)

and then you can:  
for product in @products

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 1:07am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/3 "2006-04-26T01:07:40Z")

</div>

You’ll need to do that in your model or controller:

@products = product.find(:all,  
:conditions =\> “category = 1”)

or with a variable:

@products = product.find(:all,  
:conditions =\> [“category = ?”, category\_id])

Then you can loop @products in your view.

Steve

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 1:07am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/4 "2006-04-26T01:07:45Z")

</div>

On 4/25/06, BB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the equivalent of the WHERE SQL condition in rails?
> 
> I’m thinkin this could be something like:
> 
> \<% for product in @products where category=“1” %\>
> 
> but obviously the syntax is wrong.

You can use:  
matches = @products.select {|product| product.category == ‘1’}

Also, if you know in advance that you only want products from category  
1, you can just do that in the initial find:  
@products = Product.find :all, :conditions =\> “category = ‘1’”

I assume you’ve got something other than that in mind, though.

If you want things grouped by category, you can also do:

@products.group\_by(&:category).each do |category, products|

# Do stuff with the current category

# Do stuff with the current set of products

end

Find out more here:  
[http://weblog.rubyonrails.org/articles/2006/03/01/new-in-rails-enumerable-group\_by-and-array-in\_groups\_of](http://weblog.rubyonrails.org/articles/2006/03/01/new-in-rails-enumerable-group_by-and-array-in_groups_of)

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 1:21am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/5 "2006-04-26T01:21:03Z")

</div>

Thank you all so much, I think I understand the relationship a bit  
better now and I got my app running.

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 1:38am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/6 "2006-04-26T01:38:35Z")

</div>

On Apr 25, 2006, at 3:49 PM, BB wrote:

> What is the equivalent of the WHERE SQL condition in rails?
> 
> I’m thinkin this could be something like:
> 
> \<% for product in @products where category=“1” %\>
> 
> but obviously the syntax is wrong.

for product in Product.find\_all\_by\_category(1)

# do something with product

end

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 26, 2006, 1:35am UTC](https://www.ruby-forum.com/t/sql-where-equivalent-in-rails/58797/7 "2006-04-26T01:35:05Z")

</div>

BB wrote:

> Thank you all so much, I think I understand the relationship a bit  
> better now and I got my app running.

@category.products.each do |product|  
…  
end

class Category \< ActiveRecord::Base  
has\_many :products  
end

class Product \< ActiveRecord::Base  
belongs\_to :category  
end

(1 to many relation, you can make it many to many with :through)

Gokhan  
[www.sylow.net](http://www.sylow.net)
