× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Override ActiveRecord::Base

Simone Carletti
Mar 21, 2015
<p>There are two problems here. The first, is that you are injecting a bunch of stuff into all ActiveRecord models, whereas it would be better to add the feature only to the relevant models.</p> <p>Secondary, you are breaking the MVC pattern trying to inject into the model the session context.</p> <p>What you should do instead, is to code your feature in a module, and mix the module only in the relevant models. As per the context, rather than overriding the default AR behavior, add a new method where you pass the current session context (dependency injection) and returns the model initialized with the required company, when the session is set properly and the model is company-aware.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28211685/Override%20ActiveRecord::Base/28211868">Stack Overflow</a>.</p>
comments powered by Disqus