# Find()

**URL:** <https://www.ruby-forum.com/t/find/97808>\
**Category:** Rails\
**Created:** [July 10, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/find/97808 "2006-07-10T18:23:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 10, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/find/97808/1 "2006-07-10T18:23:58Z")

</div>

Can some1 help me on syntax with the find() function.

I’m trying to pull up a table record, called chart, that has a  
particular field specified.

ie

chart has a field called record\_id, which i know. How can i find the  
record that contains chart.record\_id.

something like chart = Chart.find(:conditions =\> “record\_id = :id”)

this gives me an error. I’m passing :id, to the controller. Please  
help!!  
Thanks,  
Yngwie

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 10, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/find/97808/2 "2006-07-10T18:41:02Z")

</div>

> chart has a field called record\_id, which i know. How can i find the  
> record that contains chart.record\_id.

i’m guessing there is a

class Record  
has\_many :charts,  
class Chart  
belongs\_to :record

relashinship(by record\_id in the chart table)  
if so, and you know the id of the Record, you could find the  
corresponding chart object by putting

@record = Record.find(params[:id], :include =\> :charts)

and then take the charts out by

@charts = @record.charts

which will give you an array of all the charts that belong to a record  
with an id that is identical to the params[:id] variable.

hope it helps,

shai

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 10, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/find/97808/3 "2006-07-10T18:48:45Z")

</div>

shai wrote:

> i’m guessing there is a
> 
> class Record  
> has\_many :charts,  
> class Chart  
> belongs\_to :record

This is not the case. I have a view, called ‘charts’ that joins all  
tables called ‘owners’ with all tables called ‘records’ each. I know  
which record\_id i’m looking for. i don’t know the chart.id, so i  
beleive i need to do the search on all the charts to find the chart that  
contains the chart.record\_id that i know.

Yngwie

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 11, 2006, 7:55am UTC](https://www.ruby-forum.com/t/find/97808/4 "2006-07-11T07:55:01Z")

</div>

> This is not the case. I have a view, called ‘charts’ that joins all  
> tables called ‘owners’ with all tables called ‘records’ each. I know  
> which record\_id i’m looking for. i don’t know the chart.id, so i  
> beleive i need to do the search on all the charts to find the chart that  
> contains the chart.record\_id that i know.

is it possible for you to show the appropiate tables and their  
relashinship? do you have a charts table? i’m not really gathering from  
the information above…detailed info is well appreciated

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 11, 2006, 8:08am UTC](https://www.ruby-forum.com/t/find/97808/5 "2006-07-11T08:08:00Z")

</div>

On 7/11/06, yngwie yngwie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> something like chart = Chart.find(:conditions =\> “record\_id = :id”)

There is no string interpolation included here. The actual condition  
passed  
to the database is  
WHERE record\_id = :id

This will not work since the db has no record matching :id

You need to put it slightly different in your call

@chart = Chart.find( :all, :conditions =\> [“record\_id = ?”, :id] )

or on edge rails (I don’t think it’s included yet in gem rails)  
@chart = Chart.find(:all, :conditions =\> { :record\_id =\> :id } )

It’s not a good idea to use direct string interpolation since this  
leaves  
you open to sql injection. ie “record\_id=#{:id}”

Hope this helps

this gives me an error. I’m passing :id, to the controller. Please
