# Rails MySQL query

**URL:** <https://www.ruby-forum.com/t/rails-mysql-query/77232>\
**Category:** Rails\
**Created:** [March 6, 2006, 11:10am UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232 "2006-03-06T11:10:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [March 6, 2006, 11:10am UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232/1 "2006-03-06T11:10:27Z")

</div>

Hi,

I´m following the Depot tutorial in Agile Web D. with Rails.  
I need some help to display products thats in one specific category.  
mysql\> select \* from products where category = “jackets” does what I  
want, but I´m not sure how to make a list and display it in rails?

This code displays every product with some of its fields, but I want  
it to only display the products that has the field category set to  
“jacket”  
\<% for product in @products -%\>

  
 ![]()  

### \<%= product.brand %\> \<%= h(product.title) %\>
  
\<%= product.description %\>  
  

I would be very grateful it anyone could help me out with some  
examples or point me in the right direction.

Best regards,  
Martin S.\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [March 6, 2006, 11:22am UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232/2 "2006-03-06T11:22:34Z")

</div>

Add to your controller:

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

And the same code you have in your view will display just the products  
you  
want.  
HTH  
Juanjo

----- Original Message -----  
From: “Martin S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, March 06, 2006 11:07 AM  
Subject: [Rails] Rails MySQL query

Hi,

I´m following the Depot tutorial in Agile Web D. with Rails.  
I need some help to display products thats in one specific category.  
mysql\> select \* from products where category = “jackets” does what I  
want, but I´m not sure how to make a list and display it in rails?

This code displays every product with some of its fields, but I want  
it to only display the products that has the field category set to  
“jacket”  
\<% for product in @products -%\>

  
 ![]()  

### \<%= product.brand %\> \<%= h(product.title) %\>
  
\<%= product.description %\>  
  

I would be very grateful it anyone could help me out with some  
examples or point me in the right direction.

Best regards,  
Martin S.\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [March 6, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232/3 "2006-03-06T12:38:27Z")

</div>

Hi,

I first tried this in store\_controller.rb:

# Display the catalog, a list of all salable products.

def index  
@products = Product.find(:all, conditions =\> “category =  
'‘jackets’”)  
end

But then I got this error message:  
NameError in Store#index  
undefined local variable or method `conditions’ for #\<StoreController:  
0x23e7e40\>

tried to change that back to its original setting, and change  
product.rb instead, but this produces a blank page. It doesn’t  
display anything.

def self.salable\_items  
find(:all,  
:conditions =\> “date\_available \<= now()”,  
:conditions =\> “category = ‘jackets’”),  
:order =\> “date\_available desc”)  
end

Made store\_controller.rb look like it did to start with:  
def index  
@products = Product.salable\_items  
end

This didn’t work either.

Please let me know if you can see what I have done wrong or have any  
other suggestions! 🙂

Regards,  
Martin

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [March 6, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232/4 "2006-03-06T13:10:02Z")

</div>

I don’t think you need the ‘’ around the jackets.  
Product.find(:all, conditions =\> “category = jackets”)

That should work  
Joey  
[http://www.feedreed.com](http://www.feedreed.com)

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [March 6, 2006, 1:11pm UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232/5 "2006-03-06T13:11:47Z")

</div>

Thanks!

On Mar 6, 2006, at 1:04 PM, Juanjo Bazán wrote:

> > I first tried this in store\_controller.rb:
> > 
> > # Display the catalog, a list of all salable products.
> > 
> > def index  
> > @products = Product.find(:all, conditions =\> “category =  
> > '‘jackets’”)  
> > end
> 
> It should be :conditions (with a colon) instead of conditions.

Now it worked! 🙂

Best regards,  
Martin S.

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [March 6, 2006, 1:05pm UTC](https://www.ruby-forum.com/t/rails-mysql-query/77232/6 "2006-03-06T13:05:44Z")

</div>

> I first tried this in store\_controller.rb:
> 
> # Display the catalog, a list of all salable products.
> 
> def index  
> @products = Product.find(:all, conditions =\> “category =  
> '‘jackets’”)  
> end

It should be :conditions (with a colon) instead of conditions.
