# @record.each do |record|

**URL:** <https://www.ruby-forum.com/t/record-each-do-record/97939>\
**Category:** Rails\
**Created:** [July 11, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939 "2006-07-11T16:32:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 11, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/1 "2006-07-11T16:32:26Z")

</div>

I’m trying to ‘destroy’ a particular record of a table

From the controller side, i’ve found a list of possible records that i  
want deleted.

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

‘id’ is passed into the controller and @chart is all the possible  
records that have record\_id = id.

My problem is this. I know there is only one chart w/ record\_id = id.  
But the search that i’ve done thinks that there could be a magnitude of  
charts with record\_id = id. I need to be able to find the chart that  
has record\_id = id so i can delete this chart.

From the view side, I was able to solve a similar sitiuation with

@chart.each do |chart|  
if chart.record\_id = id  
‘delete’  
end

Is there a way to do a similar for loop from the controller side?

Yngwie

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 11, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/2 "2006-07-11T16:53:42Z")

</div>

Yngwie wrote:

> end
> 
> Is there a way to do a similar for loop from the controller side?
> 
> Yngwie

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

Matt

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 11, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/3 "2006-07-11T17:21:29Z")

</div>

> @chart = Chart.find(:first, :conditions =\> [“record\_id = ?”, param[‘id’]  
> ]
> 
> This would return you only one. @chart.destroy.
> 
> aj

Thanks, i really appreciate your timely responses.

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 11, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/4 "2006-07-11T17:15:07Z")

</div>

Yngwie wrote:

> I’m trying to ‘destroy’ a particular record of a table
> 
> From the controller side, i’ve found a list of possible records that i  
> want deleted.
> 
> ie. @chart = Chart.find(:all, :conditions =\> [“record\_id = ?”,  
> param[‘id’] ]
> 
> ‘id’ is passed into the controller and @chart is all the possible  
> records that have record\_id = id.
> 
> My problem is this. I know there is only one chart w/ record\_id = id.  
> But the search that i’ve done thinks that there could be a magnitude of  
> charts with record\_id = id. I need to be able to find the chart that  
> has record\_id = id so i can delete this chart.
> 
> From the view side, I was able to solve a similar sitiuation with
> 
> @chart.each do |chart|  
> if chart.record\_id = id  
> ‘delete’  
> end
> 
> Is there a way to do a similar for loop from the controller side?
> 
> Yngwie

How about  
@chart = Chart.find(:first, :conditions =\> [“record\_id = ?”, param[‘id’]  
]

This would return you only one. @chart.destroy.

aj

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 11, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/5 "2006-07-11T23:34:46Z")

</div>

Or

Chart.find\_by\_record\_id(params[:id]).destroy

Those find\_by\_ and find\_all\_by\_ style methods are dynamically  
generated depending on what fields are in your table.

Cheers  
-h

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 11, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/6 "2006-07-11T20:58:59Z")

</div>

Yngwie wrote:

> > @chart = Chart.find(:first, :conditions =\> [“record\_id = ?”, param[‘id’]  
> > ]
> > 
> > This would return you only one. @chart.destroy.
> > 
> > aj
> 
> Thanks, i really appreciate your timely responses.

Also:

Chart.destroy\_all [‘record\_id = ?’, params[:id]

That will delete the record directly without loading it first.

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 2:20am UTC](https://www.ruby-forum.com/t/record-each-do-record/97939/7 "2006-07-12T02:20:35Z")

</div>

Assuming your model is Chart, try:

Chart.find\_by\_id(params[:id].destroy

This doesnÂ¹t do any error checking and find\_by\_id will return a nil,  
causing  
an error if that id is not found so you might want to rescue it in case  
somebody sneaks a bad id into this code.
