Help with SQL and Ruby

I have this:

a=RpPart.find(:all)

=> [#<RpPart id: 1, product_id: 1, ...">, #<RpPart id: 2, product_id: 1
...">, #<RpPart id: 3, product_id: 2, ...>]

How can I select only de rp_parts with product_id=1?
Is there something like @repair_ticket = RepairTicket.find(???) ?

I have this:

a=RpPart.find(:all)

=> [#<RpPart id: 1, product_id: 1, ...">, #<RpPart id: 2, product_id: 1
...">, #<RpPart id: 3, product_id: 2, ...>]

a = RpPart.find(:all, :conditions => "product_id=1")

If that is ever going to be dynamic do it like this:

pid = 123
a = RpPart.find(:all, :conditions => ["product_id = ?", pid])

-philip

Philip Hallstrom wrote:

I have this:

a=RpPart.find(:all)

=> [#<RpPart id: 1, product_id: 1, ...">, #<RpPart id: 2, product_id: 1
...">, #<RpPart id: 3, product_id: 2, ...>]

a = RpPart.find(:all, :conditions => "product_id=1")

If that is ever going to be dynamic do it like this:

pid = 123
a = RpPart.find(:all, :conditions => ["product_id = ?", pid])

-philip

Thank you very much. It has been very helpful.

You can also use:

   part = RpPart.find_by_product_id(2)

If there can be more than one with the same product_id:

   part = RpPart.find_all_by_product_id(2)

Or Product.find(2).rp_parts if it’s associated like that.

Ryan Bigg wrote:

Or Product.find(2).rp_parts if it's associated like that.

>

--
Ryan Bigg
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

This way you'll *probably* execute more SQL queries. One to fetch the
product and another to fetch its rp_parts. Something to consider,
especially if performance is a concern.