Set a model attribute in the background based on another attribute?

I have an auction model with multiple attributes, two of which are current_auction: boolean and schedule_start: datetime . How can I make the boolean (default false) become true when schedule_start becomes the current time? Please tell me if you need more information. I guess I will need to use a script / runner, but I have no experience.

0


source to share


2 answers


I would not store current_auction as a property. Current_auction is rather a state dependent on schedule_start time and current time. Don't put it in the database, but add it as a method on your model that compares the sched_start value to the current time and returns true / false based on the calculation.



If you need to select based on whether it is current or not, use selection logic that depends on sched_start time and not current / not current.

0


source


Chances are you don't want this in the database as tvanfosson says.

But if you need it in a database (say you need to search for it using Sphinx), you probably need a database trigger. See MySQL 5.0 Reference Maunual for some information (assuming you are using MySQL).



For things that require more complex logic, I set up a queuing system and add a message to the queue to update this attribute in after_save

. So it will do the update, but not really wait for the update to complete before returning it to the user. I had a question on async queues and ended up using Starling and Workling.

0


source







All Articles