# Automatically delete oldest post of a specified tag

**URL:** <https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835>\
**Category:** Rails\
**Created:** [April 30, 2009, 12:12am UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835 "2009-04-30T00:12:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [April 30, 2009, 12:12am UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/1 "2009-04-30T00:12:36Z")

</div>

I have a form that allows a user to create a post with a title, body,  
image and a tag. All fields are required using validates\_presence\_of and  
has\_attached\_file in the model.  
Since every post has a tag, there is a page for each tag that has all  
the posts with that tag paginated on it. When this tag (show) view  
reaches a specified limit, I want the oldest post with that tag deleted  
as a new one is created.

This is what I think I have to do: before a post is saved, I have to  
find how many posts exist with that tag, if more than (let’s say) 20  
exist, delete the post with the oldest “updated\_at” value that belongs  
to that tag.  
In my tags (show) view I use this code to show how many posts for a tag  
exist:

[code=]\<% post.tag\_counts.each do |tag| %\>

Posts for this topic: \<%= tag.count %\>

\<% end %\>[/code] When I create a post, this is what is used in my posts\_controller currently: [code=] def create @post = Post.new(params[:post]) respond\_to do |format| if @post.save #@old\_post = Post.first #@old\_post.destroy flash[:notice] = "Post successfully created." format.html { redirect\_to(:controller =\> "tags") } else flash.now[:error] = "Error creating post." format.html { render :action =\> "new" } end end end[/code] I use this form to create a post: [code=]\<% form\_for @post, :html =\> { :multipart =\> true } do |f| %\> \<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label ‘Image’ %\>  
  
\<%= f.file\_field :image %\>

\<%= f.label :body %\>  
  
\<%= f.text\_area :body %\>

\<%= f.label ‘Topic’ %\>  
  
\<%= f.text\_field :tag\_list %\>

\<%= f.submit ‘Create’ %\>  
\<% end %\>[/code]  
Before each post is created, I need to somehow check how many posts have  
the tag that the user has entered in the form (\<%= f.text\_field  
:tag\_list %\>). So if that tag doesn’t exist, it should create the post.  
If that tag already exists, it should check how many posts for that tag  
exist and if the amount of posts that belong to it is less than 20,  
create the post, if the amount of posts that belong to that tag equal or  
is greater than 20, it should delete the post with the oldest  
‘updated\_at’ value (and create the post).

I’m not completely sure but I think I have to add this to the create  
method in my posts controller. Can someone push me in the right  
direction?

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [April 30, 2009, 2:14am UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/2 "2009-04-30T02:14:18Z")

</div>

You really need to delete the post? With :limit =\> 20 in your find is  
not enought?. If the answer is a double yes, then you should use an  
ActiveRecord callback (perhaps after\_save  
[ActiveRecord::Callbacks](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#M002084))

Regards.

Franco C…

On Apr 29, 7:12 pm, Victor V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [April 30, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/3 "2009-04-30T19:13:15Z")

</div>

The callback would be placed in the post model correct? And that  
callback would include code that would find the amount of posts in that  
tag if it exists? Then how would I use that in the post controller’s  
create method? Sorry I’m still trying to grasp MVC ☹

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [April 30, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/4 "2009-04-30T19:49:03Z")

</div>

> The callback would be placed in the post model correct?  
> Yes.  
> And that callback would include code that would find the  
> amount of posts in that tag if it exists?  
> Yes.  
> Then how would I use that in the post controller’s create  
> method?  
> When you call @post.save in the controller the appropiated callbacks  
> in the Post model are called, so you don’t need a explicit call to a  
> function or do another thing inside the model.

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [April 30, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/5 "2009-04-30T20:14:25Z")

</div>

Thank you Franco for being so helpful and understanding. I appreciate  
it.

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [May 2, 2009, 1:13am UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/6 "2009-05-02T01:13:31Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [May 1, 2009, 3:49am UTC](https://www.ruby-forum.com/t/automatically-delete-oldest-post-of-a-specified-tag/166835/7 "2009-05-01T03:49:45Z")

</div>

Hello, I’m trying to create that callback method. Here’s what I have so  
far.  
post model:

class Post \< ActiveRecord::Base  
after\_create :check\_posts\_per\_tag  
after\_create :destroy\_oldest\_post\_of\_tag  
acts\_as\_taggable #\<-- for acts\_as\_taggable\_on\_steroids  
validates\_presence\_of :body, :title, :tag\_list  
has\_many :comments

#…

def destroy\_oldest\_post\_of\_tag  
self.class.first.destroy  
end

protected #\<-- should this be protected?  
def check\_posts\_per\_tag  
@inputtag = :tag\_list #\<-- will this grab the input from the form?  
@postspertag = post.tag\_counts # maybe post.tag.count?  
if @inputtag.exists? then  
if @postspertag \>= 20 then  
# delete the oldest post of that tag needs to happen here  
post.destroy\_oldest\_post\_of\_tag  
end  
end  
end  
end

Does this look pretty close to what I described above? I’m trying to  
understand whats going on here. The comments in the code include some of  
my questions. I’m not sure if that’s how I grab the input from the form,  
and I’m also not sure if that’s how I would delete the oldest post of  
the given tag either…
