11175
(-- --)
January 9, 2008, 10:09pm
1
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
11175
(-- --)
January 9, 2008, 10:31pm
3
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.
11175
(-- --)
January 9, 2008, 10:40pm
4
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)
radar
(Ryan Bigg)
January 9, 2008, 10:43pm
5
Or Product.find(2).rp_parts if it’s associated like that.
11175
(-- --)
January 9, 2008, 11:26pm
6
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.