-
Notifications
You must be signed in to change notification settings - Fork 18
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
Association condition seems to be ignored (at least in Rails 7) #25
Comments
@stex I don't seem to reproduce this error.
I don't think that is correct notation.
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: