0

i want to activate a post in typical date(which is scheduled by user) and update data in model

for example.

Post schema
t.string :title
t.text :body
t.datetime :valid_from
t.datetime :valid_until
t.string :act_status, default: "padding #["pending", "activate", "expired"]

every post has valid_from, valid_until, and act_status

act_status has 3 phase (pending, activate, and expired)

user fill out all infos,

and when "Time.now" meets "valid_from", post will be valid to all user and it's "act_status" will change into "activate" (@post.act_status = "activate")

when "Time.now" meets "valid_until", post will invalid to other users and "act_status" will change into "expired"(@post.act_status = "expired")


my question is, how rails changes value automatically in typical date.

in my thought, i can change value when "user" acts something(fires some action such as CRUD)

but in my case, user just saves value and rails should fire that action.

i dont have any idea to solve it.

if you have some instructions or guide, please let me know

thanks..


  • additionally is it efficient way to store options of "act_status" in models?

    Post.rb

    validates :act_status, inclusion: { in: %w(padding activate finish) }
    
lee
  • 21
  • 4
  • http://stackoverflow.com/questions/33837851/ruby-on-rails-trigger-an-event-automatically-on-specific-datetime may help – margo Mar 28 '17 at 15:30

1 Answers1

1

You don't need any of this act_status, just create scopes and display the items you want.

# in your model
scope :pending, -> { where('valid_until < now()') }
scope :active, -> { where('now() between valid_from AND valid_to') }
scope :expired, -> { where('valid_from > now()') }


# in your controller
@pending_posts = Post.pending
@active_posts = Post.active
@expired_posts = Post.expired
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • 1
    thanks iceman. it helped me a lot. i thought act_status will necessary when i want user could activate only one post in period. for example, two post can't activate in same time. if i want to add this idea, do you have any advise? – lee Mar 28 '17 at 15:39
  • Good to hear, just mark it as an answer if it solved your problem. – Eyeslandic Mar 28 '17 at 15:57