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 H. 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)

Ryan B. wrote:

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

On Jan 10, 2008 9:10 AM, Jeremy Weiskotten
[email protected]
wrote:


Ryan B.
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.

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

On Jan 10, 2008 9:10 AM, Jeremy Weiskotten
[email protected]
wrote:


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs