I''m trying to ''destroy'' a particular record of a table>From the controller side, i''ve found a list of possible records that iwant 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 -- Posted via http://www.ruby-forum.com/.
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 >try: @chart = @chart = Chart.find(:all, :conditions => ["record_id = ?", params[:id]]).destroy Matt
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? > > YngwieHow about @chart = Chart.find(:first, :conditions => ["record_id = ?", param[''id''] ] This would return you only one. @chart.destroy. aj -- Posted via http://www.ruby-forum.com/.
> @chart = Chart.find(:first, :conditions => ["record_id = ?", param[''id''] > ] > > This would return you only one. @chart.destroy. > > ajThanks, i really appreciate your timely responses. -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
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 On 7/11/06, Yngwie <mdalsant@gmail.com> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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. On 7/11/06 7:32 AM, "Yngwie" <mdalsant@gmail.com> 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-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/00f36fb0/attachment-0001.html