Forum: Ruby on Rails Automatically delete oldest post of a specified tag

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
aa b. (Guest)
on 2009-04-30 02:12
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| %>
  <p>Posts for this topic: <%= tag.count %></p>
<% 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 %><br />
  <%= f.text_field :title %>

  <%= f.label 'Image' %><br />
  <%= f.file_field :image %>

  <%= f.label :body %><br />
  <%= f.text_area :body %>

  <%= f.label 'Topic' %><br />
  <%= 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?
Franco C. (Guest)
on 2009-04-30 04:14
(Received via mailing list)
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/Ca...)

Regards.

Franco C..

On Apr 29, 7:12 pm, Victor V. <removed_email_address@domain.invalid>
aa b. (Guest)
on 2009-04-30 21:13
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 :(
Franco C. (Guest)
on 2009-04-30 21:49
(Received via mailing list)
> 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..
aa b. (Guest)
on 2009-04-30 22:14
Thank you Franco for being so helpful and understanding. I appreciate
it.
aa b. (Guest)
on 2009-05-01 05:49
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...
aa b. (Guest)
on 2009-05-02 03:13
Anyone?
This topic is locked and can not be replied to.