Open stex opened 2 years ago
@Stex I don't seem to reproduce this error.
has_many :mandatory_tags, -> { merge(Tagging.mandatory) }, through: :taggings, source: :tag
I don't think that is correct notation. Tags
are extracted with this association, but Tagging
scope is merged.
but the same happens when defining a condition on the
tags
table itself.
Please provide that example.
Here's an example
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem 'activerecord', '~> 7.0.0'
gem 'sqlite3'
gem 'where_exists', '2.0.1'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.boolean :archived, default: false, null: false
end
create_table :comments, force: true do |t|
t.integer :post_id
t.integer :commentator_id
end
create_table :commentators, force: true do |t|
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :commentator
end
class Commentator < ActiveRecord::Base
has_many :comments
has_many :posts, -> { where(archived: false) }, through: :comments
end
class BugTest < Minitest::Test
def test_where_exists
post = Post.create!
archived_post = Post.create! archived: true
commentator = Commentator.create! posts: [post]
commentator2 = Commentator.create! posts: [archived_post]
commentator3 = Commentator.create!
assert_equal [commentator], Commentator.where_exists(:posts).to_a # fail: also includes commentator2
assert_equal [commentator2, commentator3], Commentator.where_not_exists(:posts).to_a # fail: does not include commentator2
end
end
Hey!
First of all, thanks a lot for this gem and for saving me from having to write pure SQL again in a Rails app :)
I ran into a problem today and it seems like the "Does it take into account default association condition" part of the README isn't fully correct.
An example model structure is fairly easy:
When calling the helper associations for mandatory, optional and excluded tags directly, everything works as it should:
However, when using
where_not_exists
, it simply swallows the default condition:At first I thought that this might be problem with the condition being on the
through
table, but the same happens when defining a condition on thetags
table itself.I could provide an example Rails 7 application if that helps.