First of all, try to follow ruby & rails conventions. If you call module CheckPulse
it's file name should be check_pulse.rb
. It will help you to avoid require_relative
. Second, in spec and in module you interact with different ruby objects which are both writing to same row in database. And if you update one object another will not be updated synchronously. You need to reload it manually by calling machine.reload
in spec.
Also few tips to your code:
- never use
all.each
. It will stuck if you have a 2k+ records in your database. For iterating all database records usefind_each
- instead of loading each record and update it, you could update all records with
update_all
->Machine.update_all(:column_1 => value_1, :column_2 => value_3)
- avoid round brackets in methods without arguments