I think I’m having a brain freeze here but I cannot figure out the best way to do this… Thanks in advance for the help.
I have a some fields in my location table are updated by an external script every 5 minutes. If they’re not updated for more than 1 hour, an email will be sent out to the site admins.
That was reasonably straightforward getting a list of dead locations:
scope :not_responding, lambda {
where('last_heartbeat < ?', Time.now - 1.hour )
}
What I’m stuck on should be very simple…. All I want to do is change the styling of the location name so it’s red if it’s not responding.
%td
- @locations.each do |locations|
= if ('locations.last_heartbeat < ?', Time.now - 1.hour )
red
-else
green
But that throws an error (unexpected ‘,’)
Can anyone suggest how to move this into my model??
— UPDATED —
I’m not sure if this is a bug or what but I’m seeing some bizarre results.
Using answer below, I’ve added this:
def not_responding?
last_heartbeat < (Time.now - 1.hour)
end
And call using:
- if location.not_responding?
red
- else
green
However, this gave me an error about ‘<‘ being undefined.
In the end, I had to do this to get it to work. All seems ridiculous….
def not_responding
last_heartbeat <=> (Time.now - 1.hour)
end
And in the view:
- if (hotspot.not_responding_two == -1)
red
- else
green
Does not make sense as the follow test was ok:
def test?
Time.now > (Time.now - 1.hour)
end
Probably the easiest way would be something like this:
I threw in a question mark because this is a Boolean method. And in your view:
You need to call a method on the location object, and in this case you’re calling the one we just defined, which will return true or false depending on when the last heartbeat was.