Automatically delete oldest post of a specified tag

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)

Regards.

Franco Catena.

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:

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

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

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

Anyone?