Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7.2 compatibility: Add README.md note about custom audit table names #726

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ Audited.config do |config|
end
```

#### Changing the table name

If you use a custom audit model _and_ define a different table name inside it, you must mark the `Audited::Audit` superclass as abstract. Otherwise you may get exceptions from attempts to access the missing `audits` table, since ActiveRecord still "sees" a valid, non-abstract model class in `Audited::Audit` and may attempt to access information about its inferred related table.
Copy link
Author

@pond pond Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm aware this suggests an STI-related solution to a non-STI problem, but in this specific case I think it does make sense; we don't want the base class to be instantiated ever and we definitely want ActiveRecord to use the subclass's table name only.

TL;DR, there's some confusing ActiveRecord documentation around inheritance that could do with tidying up. I'd do a PR for that, but I don't actually know what the intended behaviour is anymore given recent Rails changes.


Due to complexities of autoloading, this is most reliably achieved inside the subclass itself:
```ruby
class CustomAudit < Audited::Audit
self.table_name = 'audit_trails'
superclass.abstract_class = true
end
```

### Enum Storage

In 4.10, the default behavior for enums changed from storing the value synthesized by Rails to the value stored in the DB. You can restore the previous behavior by setting the store_synthesized_enums configuration value:
Expand Down