Joy
Joy

Reputation: 4483

Time driven state transition in ruby on rails

I am new to Ruby on Rails. I have used one model Student in my application. The student can have two states valid and invalid which I implemented with state machine. Each student object has one field named term_end_date. I want to do the following: if term_end_date is equal to Date.today, the state transition :valid => :invalid should take place.

So I am asking if anyone can give any idea how to implement that. Thanks in advance.

Upvotes: 1

Views: 111

Answers (1)

zwippie
zwippie

Reputation: 15515

What if you don't store the state, but determine the state on runtime? You could use scopes to fetch Students with the right state:

scope :valid, -> { where('term_end_date < ?', Time.now) }
scope :invalid, -> { where('term_end_date >= ?', Time.now) }

def state
  term_end_date < Time.now ? :valid : :invalid
end

Call like:

Student.valid.where(class: 'Biology')
Student.find(13).state

Upvotes: 1

Related Questions