Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to disable belongs_to :touch option in Rspec tests for Rails models?
    primarykey
    data
    text
    <p>Having a large model stack and using doll caching techniques extensively, one ends up with lots of parent models been "touched" after a model update. </p> <p>While testing, this seems to be a time waster unless you try to test that feature specifically. </p> <p>Is there a way to prevent models to <code>touch</code> their <code>belongs_to</code> associations for the test environment or at a test level?</p> <p>UPDATE 1:</p> <p>My first attempt to the case would be to </p> <pre><code># /config/initializers/extensions.rb # class ActiveRecord::Base def self.without_touch_for_association(association_name, &amp;block) association_name = association_name.to_sym association = self.reflect_on_all_associations(:belongs_to).select { |reflection| reflection.name == association_name }.first options = association.options association.instance_variable_set :@options, options.except(:touch) yield association.instance_variable_set :@options, options end end Post.without_touch_for_association(:user) do Post.last.save end </code></pre> <p>Of course, no success and saving <code>Post.last</code> still touches it's <code>User</code>.</p> <p>UPDATING RATIONALE: </p> <p>I understand and agree that this approach may be a source of bugs and it's not a good practice at all. The thing is that I have a huge suite with lots of both integration and unit tests. Doll caching also gets deep in the model tree. Every time I look at the logs, I see a significant % of touch-related queries. I know the best way would be optimizing the unit tests to add more mocking and stubbing and less persistence. Solving the issue within integration tests is more difficult. </p> <p>In any case, I'm asking this question for the sake of learning and research. I am interested in exploring the potential speed improvements of this technique.</p> <p>SOLUTION: see my own answer below for the working code.</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.
 

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