Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you would like to handle this on the Rails stack itself, one way I currently handle a similar situation is using an ActiveRecord Callback method. Documentation for that is available <a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#M001381" rel="nofollow noreferrer">here</a>. In your model where the attribute is originally being modified you could use the after_validation_on_create callback:</p> <pre><code>after_validation_on_create :toggle_other_table_asset def toggle_other_table_asset if self.in_use sibling.in_use = 1 else sibling.in_use = 0 end sibling.save end </code></pre> <p>You also might want to consider using an ActiveRecord transaction to ensure the integrity of your data, more information is found <a href="http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html" rel="nofollow noreferrer">here</a>. </p> <p>The best piece of advice I can give you is to consider the potential drawbacks to ensuring everything is done on the application layer. There's also drawbacks involved in coupling your data in this fashion. Perhaps you should think about refactoring your models to avoid duplication of data. </p> <p>Examples of where things can go wrong include maintenance scripts which run on the weekends that clean up your database, or interacting with your underlying database's command line interface and modifying the culprit attributes manually.. without having a database TRIGGER programmed, you're running the risk of damaging the integrity of your data.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload