Automatically delete oldest post of a specified tag


#1

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?


#2

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


#3

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 :frowning:


#4

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…


#5

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


#6

Anyone?


#7

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…