ruby-on-railsrspecdatabase-cleaner

Database Cleaner strategy is cleaning the table mention in except block of truncation strategy


I have the following database cleaner strategy

DatabaseCleaner.strategy = :truncation, {:except => %w[roles, users, other_tables]} 

It's still cleaning the roles table and I don't have any dependent destroy on the models that role belongs to. This does not look like intended behavior. Other than Role table, data is persistent across.


Solution

  • I'd first make sure to define the array correctly, please check in the console what it evaluates to:

    > %w[roles, users, other_table]
    => ["roles,", "users,", "other_table"]
    

    There's (probably) no table called roles, (with a comma at the end).

    Correct that to:

    DatabaseCleaner.strategy = :truncation, {:except => %w[roles users other_tables]}
    

    because:

    > %w[roles users other_table]
    => ["roles", "users", "other_table"]