@record.each do |record|

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

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

@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.

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

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

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.

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.

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