Ruby

Ruby

Made by DeepSource

Redundant ActiveRecord foreign Key RB-LI1105

Anti-pattern
Major
Autofix

Aside from being unnecessary, setting the foreign key prevents the inverse association from being inferred automatically, which can lead to unnecessary queries.

Bad practice

class Post
  has_many :comments, foreign_key: 'post_id'
end

class Comment
  belongs_to :post, foreign_key: 'post_id'
end

Recommended

class Post
  has_many :comments
end

class Comment
  belongs_to :author, foreign_key: 'user_id'
end

References

  1. ActiveRecord associations