You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have two models that are joined by a has_and_belongs_to_many relationship
# app/models/foo.rb
class Foo < ApplicationRecord
has_and_belongs_to_many :bars
end
# app/models/bar.rb
class Bar < ApplicationRecord
has_and_belongs_to_many :foos
end
And an additional class that overrides ActiveRecord::Base#table_name_prefix in order to allow us to join associations between this application and another application that heavily relies on this one to generate some reports comparing data between the two applications.
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.table_name_prefix
Rails.configuration.database_configuration[Rails.env]['database'] + '.'
end
end
Using the joined table association between foos and bars (foos_bars) produces the correct joined table in our queries:
`our-database`.`foos_bars`
Issue
We have an existing model spec for the Foo model that checks that the has_and_belongs_to_many association exists which is now failing after overriding the self.table_name_prefix method.
RSpec.describe Foo, type: :model do
it { should have_and_belong_to_many :bars }
end
Expected Foo to have a has_and_belongs_to_many association called bars (join table our-database_test.foos_database.bars doesn't exist)
As you can see, the table name that shoulda_matchers expects is incorrect. It should be expecting our-database_test.foos_bars.
I have already confirmed that Rails produces the correct joined table name so I believe this is a shoulda_matchers bug.
The text was updated successfully, but these errors were encountered:
kayla-glick
changed the title
"should have_and_belong_to_many" produces incorrect joined table when overriding ActiveRecord::Base#table_name_prefix
"should have_and_belong_to_many" produces incorrect joined table name when overriding ActiveRecord::Base#table_name_prefix
May 23, 2019
Context
I have two models that are joined by a
has_and_belongs_to_many
relationshipAnd an additional class that overrides
ActiveRecord::Base#table_name_prefix
in order to allow us to join associations between this application and another application that heavily relies on this one to generate some reports comparing data between the two applications.Using the joined table association between
foos
andbars
(foos_bars
) produces the correct joined table in our queries:Issue
We have an existing model spec for the Foo model that checks that the
has_and_belongs_to_many
association exists which is now failing after overriding theself.table_name_prefix
method.As you can see, the table name that
shoulda_matchers
expects is incorrect. It should be expectingour-database_test.foos_bars
.I have already confirmed that Rails produces the correct joined table name so I believe this is a
shoulda_matchers
bug.The text was updated successfully, but these errors were encountered: