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/ApplicationModel should ignore db/migrate/** #1342

Closed
searls opened this issue Aug 26, 2024 · 2 comments · Fixed by #1349
Closed

Rails/ApplicationModel should ignore db/migrate/** #1342

searls opened this issue Aug 26, 2024 · 2 comments · Fixed by #1349
Labels
enhancement New feature or request

Comments

@searls
Copy link

searls commented Aug 26, 2024

It's a common practice to define AR models inside of migrations in order to retain forward compatibility of the migration by avoiding loading any code from app/ (gems like strong_migrations and my own good_migrations are designed to enforce this).

Example from the codebase I'm working on:

class AddDefaultTrackingValuesToMovements < ActiveRecord::Migration[7.2]
  module Build
    class Movement < ActiveRecord::Base
      self.table_name = "build_movements"
    end
  end

  def change
    Build::Movement.update_all(default_tracking_type: nil)

    change_table :build_movements do |t|
      t.jsonb :default_tracking_values, null: true
    end
  end
end

This results in:

db/migrate/20240809113624_add_default_tracking_values_to_movements.rb:3:22: Rails/ApplicationRecord: Models should subclass `ApplicationRecord`.

Of course, if we fixed this by extending from ApplicationRecord, that'd load the model from app/models/application_record.rb, which could change in a way such that the migration couldn't be safely run again in the future. I think the cop should ignore anything under db/migrate/** as a result

searls added a commit to standardrb/standard-rails that referenced this issue Aug 26, 2024
@Earlopain
Copy link
Contributor

This makes sense to me. Do you want to open a PR?

@rubocop rubocop deleted a comment Aug 26, 2024
@rubocop rubocop deleted a comment Aug 26, 2024
@koic koic added the enhancement New feature or request label Aug 27, 2024
@koic koic closed this as completed in #1349 Sep 4, 2024
@koic
Copy link
Member

koic commented Sep 7, 2024

This issue has been resolved in RuboCop Rails 2.26.1. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@koic @searls @Earlopain and others