# Deleting model objects

**URL:** <https://www.ruby-forum.com/t/deleting-model-objects/144056>\
**Category:** Rails\
**Created:** [July 24, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/deleting-model-objects/144056 "2008-07-24T22:57:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 24, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/deleting-model-objects/144056/1 "2008-07-24T22:57:28Z")

</div>

def remove\_req  
@posts = Post.find\_all\_by\_requirement\_id(params[:id])

```
@posts.each do |post|
  post.destroy
end

redirect_to :action => :index

```

end

\<%= link\_to ‘Remove’, :url =\> { :action =\> ‘remove\_req’ },  
:id=\>post.requirement\_id, :method =\> :delete %\>

Doesn’t work, any tips?

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 24, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/deleting-model-objects/144056/2 "2008-07-24T23:14:35Z")

</div>

Justin To wrote:

> def remove\_req  
> @posts = Post.find\_all\_by\_requirement\_id(params[:id])
> 
> ```
> @posts.each do |post|
> post.destroy
> end
> 
> redirect_to :action => :index
> 
> ```
> 
> end
> 
> \<%= link\_to ‘Remove’, :url =\> { :action =\> ‘remove\_req’ },  
> :id=\>post.requirement\_id, :method =\> :delete %\>
> 
> Doesn’t work, any tips?
> 
> Thanks!

Try this…

Post.destroy params[:id]

If you don’t have any logic around the destroy (before\_destroy,  
:dependent =\> :destroy, etc.), Post.delete params[:id] would be a little  
faster.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 25, 2008, 12:30am UTC](https://www.ruby-forum.com/t/deleting-model-objects/144056/3 "2008-07-25T00:30:36Z")

</div>

remove\_req.rjs  
page.replace\_html :main-body, :partial =\> ‘requirement\_list’

view:  
\<%= link\_to\_remote ‘Remove’, :url =\> { :action =\> ‘remove\_req’ }, :with  
=\> “‘id=#{post.requirement\_id}’” %\>

control:  
def remove\_req  
Post.delete\_all “requirement\_id = #{params[:id]}”  
respond\_to do |format|  
format.html { redirect\_to :action =\> :index }  
format.js { flash[:notice] = “ok” }  
end  
end

It doesn’t replace the html =(

Suggestions?

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 25, 2008, 12:48am UTC](https://www.ruby-forum.com/t/deleting-model-objects/144056/4 "2008-07-25T00:48:17Z")

</div>

> It doesn’t replace the html =(
> 
> Suggestions?
> 
> Thanks!

Your Ajax request may be failing. Take a look at your log, or the  
Firebug console if you use it.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 25, 2008, 4:36am UTC](https://www.ruby-forum.com/t/deleting-model-objects/144056/5 "2008-07-25T04:36:29Z")

</div>

Justin To wrote:

> \<%= link\_to ‘Remove’, :url =\> { :action =\> ‘remove\_req’ },  
> :id=\>post.requirement\_id, :method =\> :delete %\>

Put the :id inside the :url. From outside, it might be the HTML id.

> Doesn’t work, any tips?

Put the deleter into a class method on Post.

Write a unit test for it.

(Actually, write the test first, then put the deleter in, but that’s an  
advanced  
topic…)

Get the test to pass, by detecting Post.find\_by\_requirement\_id() returns  
nil.

Don’t use find\_all\_by\_requirement\_id if your params[:id] can only have  
one.

Then write a “functional” test checking that your action deletes the  
target record.

And don’t use :method =\> :delete in your link. That links to a magic  
HTTP verb  
DELETE, which you probably are not implementing. Try :method =\> :post.

–  
Phlip
