# Help with SQL and Ruby

**URL:** <https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531>\
**Category:** Rails\
**Created:** [January 9, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531 "2008-01-09T23:09:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [January 9, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531/1 "2008-01-09T23:09:36Z")

</div>

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(???) ?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [January 9, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531/2 "2008-01-09T23:20:31Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [January 9, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531/3 "2008-01-09T23:31:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [January 9, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531/4 "2008-01-09T23:40:33Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [January 10, 2008, 12:26am UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531/5 "2008-01-10T00:26:49Z")

</div>

Ryan B. wrote:

> Or Product.find(2).rp\_parts if it’s associated like that.
> 
> On Jan 10, 2008 9:10 AM, Jeremy Weiskotten  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > >
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](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.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [January 9, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/help-with-sql-and-ruby/125531/6 "2008-01-09T23:43:23Z")

</div>

Or Product.find(2).rp\_parts if it’s associated like that.

On Jan 10, 2008 9:10 AM, Jeremy Weiskotten  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
