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
Is there a way to reference tables from outer scopes inside subquery?
Reproduction
require'bundler/inline'require'minitest/spec'require'minitest/autorun'gemfiletruedosource'https://rubygems.org'gem'activerecord','~> 5.0.0'# which Active Record version?gem'sqlite3'gem'baby_squeel',github: 'rzane/baby_squeel'endActiveRecord::Base.establish_connection(adapter: 'sqlite3',database: ':memory:')ActiveRecord::Schema.definedocreate_table:accounts,force: truedo |t|
t.string:nameendcreate_table:songs,force: truedo |t|
t.string:nameendcreate_table:account_songs,force: truedo |t|
t.integer:account_idt.integer:song_idendcreate_table:playlists,force: truedo |t|
t.integer:account_idt.integer:song_idendendclassSong < ActiveRecord::Basehas_many:account_songs,:dependent=>:destroy,:inverse_of=>:songhas_many:accounts,:through=>:account_songshas_many:playlists,:dependent=>:nullifyendclassAccount < ActiveRecord::Basehas_many:account_songs,:dependent=>:destroy,:inverse_of=>:accounthas_many:songs,:through=>:account_songshas_many:playlists,:dependent=>:nullifyendclassAccountSong < ActiveRecord::Basebelongs_to:account,:inverse_of=>:account_songsbelongs_to:song,:inverse_of=>:account_songsendclassPlaylist < ActiveRecord::Basebelongs_to:accountbelongs_to:songendclassBabySqueelTest < Minitest::Specit'works'do# global scopescope=AccountSong.all# filters defined down the road, in filtering classes, in this case: was this song already played:# `account_songs` here is coming from the global scopesubquery=Playlist.where.has{ |t| (t.account_id=t.account_songs.account_id) & (t.song_id == t.account_songs.song_id)}scope=scope.where.has{exists(subquery)}# result after applying all filtersscope.to_sql.must_equal%{ SELECT "account_songs".* FROM "account_songs" WHERE EXISTS ( SELECT "playlists".* FROM "playlists" WHERE "playlists"."account_id" = "account_songs"."account_id" AND "playlists"."song_id" = "account_songs"."song_id" ) }.squishendend
Here, account_songs is undefined when creating subquery.
EDIT: Fixed singular table names to be plural again.
The text was updated successfully, but these errors were encountered:
Issue
Is there a way to reference tables from outer scopes inside subquery?
Reproduction
Here, account_songs is undefined when creating
subquery
.EDIT: Fixed singular table names to be plural again.
The text was updated successfully, but these errors were encountered: