sql - Archive Posts after set time with ruby and rails -


i want archive (or delete) posts after 24hours. possible ? when user logs in after 24hours previous post not shown instead link create new post next 24hours. not matter if previous posts gets deleted love learn to both ways. cheers

one way without having alter or destroy data post use scope on record. example, like:

scope :recent_posts, lambda { where("created_at <= ?", (time.zone.now - 24.hours).to_s(:db)) } 

a couple things understand this:

  1. any place want have recent posts appear, have use .recent_posts scope
  2. the lambda needed because causes current time (time.now) evaluated @ run time every time scope invoked; doing no lambda make evaluation of time.now static , not change on time.
  3. the .to_s(:db) ensures timestamp formatted correctly evaluation database.
  4. it assumes have timestamps on posts table.
  5. it possible apply default_scope used every query against model except explicitly excluded. regarded bad practice because it's easy make mistakes not realizing scope being applied implicitly.

it possible archive records database. without knowing table structure it's hard how it, in general have 2 types of options: 1. use rake task scheduled run periodically (like every night) finds records should archived, creates archive record in new table , destroys record in active table. 2. create logic within database (like stored procedure) has logic similar above, , again use kind of scheduler trigger it.

the first easiest, understandable way it, not performant large datasets. second perform better, introduces additional complexity putting application code in rails , other application code in database tier.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -