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

has_and_belongs_to_many #26

Open
chumakoff opened this issue Mar 10, 2021 · 1 comment
Open

has_and_belongs_to_many #26

chumakoff opened this issue Mar 10, 2021 · 1 comment

Comments

@chumakoff
Copy link

Whenever I update a main record, versions for hundreds associated through 'has_and_belongs_to_many' records are created.
Is that an expected behavior?

class AnyModel < ApplicationRecord
  has_and_belongs_to_many :any_associations
end

record = AnyModel.last
record.any_associations.count # => 999999999999
record.touch  # this will result in 99999999999 "INSERT INTO `version_associations" SQL queries
@westonganger
Copy link
Owner

Yes this is probably what we are forced to do with HABTM relationships. I wouldnt recommend using HABTM relationships ever really, try using a has_many through relationship

Alternatively depending on the use case it may be an idea to add has_paper_trail only: [:revision_number] or something on the parent model to limit the number of version records created.

@westonganger westonganger changed the title Problem with has_and_belongs_to_many has_and_belongs_to_many Mar 31, 2021
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

2 participants