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

Chained associated against pg_scope and regular scope fail #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,71 @@
end
end

context "chained to a cross-table scope and with associated_against" do
with_model :House do
table do |t|
t.references :person
t.string :city
t.string :street
end

model do
belongs_to :person
end
end

with_model :Person do
table do |t|
t.string :name
end

model do
include PgSearch
has_many :houses
pg_search_scope :with_house_in_street, associated_against: {
houses: [:street]
}
scope :with_house_in_city, ->(city) {
joins(:houses).where(House.table_name.to_sym => {city: city})
}
end
end

it "works when the other scope is last" do
house_in_duluth_street_a = House.create!(city: "Duluth", street: "Street a")
house_in_duluth_street_b = House.create!(city: "Duluth", street: "Street b")
house_in_sheboygan_street_c = House.create!(city: "Sheboygan", street: "Street c")

bob_in_duluth_street_a =
Person.create!(name: "Bob", houses: [house_in_duluth_street_a])
bob_in_duluth_street_b =
Person.create!(name: "Bob", houses: [house_in_duluth_street_b])
sally_in_sheboygan_street_c =
Person.create!(name: "Sally", houses: [house_in_sheboygan_street_c])

results = Person.with_house_in_street("Street a").with_house_in_city("Duluth")
expect(results).to include bob_in_duluth_street_a
expect(results).not_to include [bob_in_duluth_street_b, sally_in_sheboygan_street_c]
end

it "works when the other scope is first" do
house_in_duluth_street_a = House.create!(city: "Duluth", street: "Street a")
house_in_duluth_street_b = House.create!(city: "Duluth", street: "Street b")
house_in_sheboygan_street_c = House.create!(city: "Sheboygan", street: "Street c")

bob_in_duluth_street_a =
Person.create!(name: "Bob", houses: [house_in_duluth_street_a])
bob_in_duluth_street_b =
Person.create!(name: "Bob", houses: [house_in_duluth_street_b])
sally_in_sheboygan_street_c =
Person.create!(name: "Sally", houses: [house_in_sheboygan_street_c])

results = Person.with_house_in_city("Duluth").with_house_in_street("Street a")
expect(results).to include bob_in_duluth_street_a
expect(results).not_to include [bob_in_duluth_street_b, sally_in_sheboygan_street_c]
end
end

it "returns an empty array when a blank query is passed in" do
ModelWithPgSearch.create!(:content => 'foo')

Expand Down