Forum: Ruby on Rails SQL WHERE equivalent in rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
511591894657d7918ecdcaf13a28f40e?d=identicon&s=25 BB (Guest)
on 2006-04-26 00:49
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.
068f60670a8f31323854f945105702db?d=identicon&s=25 Dick Dishkuvek (dishkuvek)
on 2006-04-26 01:03
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
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-04-26 01:07
(Received via mailing list)
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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-04-26 01:07
(Received via mailing list)
On 4/25/06, BB <bbsaid@sfsu.edu> 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/...
511591894657d7918ecdcaf13a28f40e?d=identicon&s=25 BB (Guest)
on 2006-04-26 01:21
Thank you all so much, I think I understand the relationship a bit
better now and I got my app running.
54077fb03c95861af85880218cf7aef2?d=identicon&s=25 Gokhan Arli (sylow)
on 2006-04-26 01:35
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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-26 01:38
(Received via mailing list)
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 Mornini
This topic is locked and can not be replied to.