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

Cop suggestion: PreferCreateJoinTable #334

Open
connorshea opened this issue Jan 3, 2019 · 4 comments
Open

Cop suggestion: PreferCreateJoinTable #334

connorshea opened this issue Jan 3, 2019 · 4 comments

Comments

@connorshea
Copy link

connorshea commented Jan 3, 2019

In a migration you can create a join table in two ways, with create_table and id: false, or create_join_table.

# These are equivalent
create_table :games_genres, id: false do |t|
  t.integer :game_id
  t.integer :genre_id
end

create_join_table :games, :genres do |t|
  t.integer :game_id
  t.integer :genre_id
end

Source: https://guides.rubyonrails.org/association_basics.html#creating-join-tables-for-has-and-belongs-to-many-associations

I'd like a cop that prefers the create_join_table syntax over id: false.

The only problem would be, I'm not sure if there's a valid reason to use id: false besides creating a join table?

@mikegee
Copy link

mikegee commented Jan 3, 2019

I'm not sure if there's a valid reason to use id: false besides creating a join table?

Not if the Rails developers control the database schema. But, sometimes we write migrations to mirror a an existing DB schema just for local development. In that case, I'd still be ok with an offense here. We're well aware of our deviation from standard practice when we're working in this scenario.

@koic
Copy link
Member

koic commented Apr 6, 2023

Since this is a matter of coding style, I think it can be implemented if the style guide accepts it. I'll transfer it as a style guide issue to the repo.

@koic koic transferred this issue from rubocop/rubocop-rails Apr 6, 2023
@pirj
Copy link
Member

pirj commented Apr 14, 2023

create_join_table doesn't need t.integer for FKs, the original Rails Guide has t.index.

@pirj
Copy link
Member

pirj commented Apr 14, 2023

I'm not sure if there's a valid reason to use id: false besides creating a join table?

Natural PK?

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

No branches or pull requests

4 participants