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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

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.” [email protected]
To: [email protected]
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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

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! :slight_smile:

Regards,
Martin

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

That should work
Joey
http://www.feedreed.com

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! :slight_smile:

Best regards,
Martin S.

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.