I'm building an app where users can decide when their posts will be deleted, but I don't know how to do it in Rails 3.
Asked
Active
Viewed 721 times
1
-
1You will have to provide more info, like what authentication system etc... – errorhandler Apr 16 '11 at 09:26
-
I'm using Devise. It's a simple app. where Users just add fields: Name Price Description Deadtime – Sudoman Apr 16 '11 at 09:32
-
When to delete posts? Do you mean deleting records based on schedule (as in Deadtime field)? – edthix Apr 16 '11 at 11:03
-
yes, your are right, deadtime is a field according to which records will be deleted – Sudoman Apr 16 '11 at 19:04
3 Answers
1
Sometimes you can inactivate a post rather than executing the database command "delete". If you inactivate, then you'll keep statistics and comments.
class Post
attr_protected :published_on, :deleted_on
belongs_to :user
scope :published, lambda{ where("published_on >= ?", Time.now).where("deleted_on IS NULL or deleted_at < ?", Time.now)}
end
in your controller later:
@posts = @user.posts.published.paginate(params[:page])

Jesse Wolgamott
- 40,197
- 4
- 83
- 109
0
I assume you want the posts to be automatically deleted on a certain date(time) and your users are able to set that date(time). To achieve that you must use cron jobs. Here you can find more about it.
Another (real dirty) solution is to execute a method each time your app is accessed and look for the posts that needed to be deleted.