效果很好:
Client.joins(:month_payments).where(month_payments:{date:“ 2019-12-02”})
或
Client.joins(:month_payments).search(“ Sara”)
如果尝试使用pg_search为什么不起作用:
Client.joins(:month_payments).where(month_payments:{date:“ 2019-12-02”})。search(“ Sara Connor”)
Client Load (2.4ms) SELECT "clients".* FROM "clients" INNER JOIN "month_payments" "month_payments_clients" ON "month_payments_clients"."client_id" = "clients"."id" INNER JOIN (SELECT "clients"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("clients"."name"::text, '')) || to_tsvector('simple', coalesce("clients"."phone_number"::text, '')) || to_tsvector('simple', coalesce(pg_search_e49121e1954ad241e59d46.pg_search_b96d7c675f96dc5e635104::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_12c411d94675b7f72bc460::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_db73ee63dc62e95d223184::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a449af3665b9d7d8aaec7d::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a712b883f3cd839eb70c1e::text, ''))), (to_tsquery('simple', ''' ' || 'Sara' || ' ''' || ':*') || to_tsquery('simple', ''' ' || 'Connor' || ' ''' || ':*')), 0)) AS rank FROM "clients" LEFT OUTER JOIN (SELECT "clients"."id" AS id, string_agg("month_payments"."payment_note"::text, ' ') AS pg_search_b96d7c675f96dc5e635104 FROM "clients" INNER JOIN "month_payments" ON "month_payments"."client_id" = "clients"."id" GROUP BY "clients"."id") pg_search_e49121e1954ad241e59d46 ON pg_search_e49121e1954ad241e59d46.id = "clients"."id" LEFT OUTER JOIN (SELECT "clients"."id" AS id, string_agg("addresses"."address_1"::text, ' ') AS pg_search_12c411d94675b7f72bc460, string_agg("addresses"."town"::text, ' ') AS pg_search_db73ee63dc62e95d223184, string_agg("addresses"."state"::text, ' ') AS pg_search_a449af3665b9d7d8aaec7d, string_agg("addresses"."zip_code"::text, ' ') AS pg_search_a712b883f3cd839eb70c1e FROM "clients" INNER JOIN "addresses" ON "addresses"."client_id" = "clients"."id" GROUP BY "clients"."id") pg_search_859b556cd4a924628ee183 ON pg_search_859b556cd4a924628ee183.id = "clients"."id" WHERE ((to_tsvector('simple', coalesce("clients"."name"::text, '')) || to_tsvector('simple', coalesce("clients"."phone_number"::text, '')) || to_tsvector('simple', coalesce(pg_search_e49121e1954ad241e59d46.pg_search_b96d7c675f96dc5e635104::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_12c411d94675b7f72bc460::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_db73ee63dc62e95d223184::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a449af3665b9d7d8aaec7d::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a712b883f3cd839eb70c1e::text, ''))) @@ (to_tsquery('simple', ''' ' || 'Sara' || ' ''' || ':*') || to_tsquery('simple', ''' ' || 'Connor' || ' ''' || ':*')))) AS pg_search_8f4d9088121acfab37f4a5 ON "clients"."id" = pg_search_8f4d9088121acfab37f4a5.pg_search_id WHERE "month_payments"."date" = $1 ORDER BY pg_search_8f4d9088121acfab37f4a5.rank DESC, "clients"."id" ASC LIMIT $2 [["date", "2019-12-02"], ["LIMIT", 11]]
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: invalid reference to FROM-clause entry for table "month_payments")
LINE 1: ..._search_8f4d9088121acfab37f4a5.pg_search_id WHERE "month_pay...
^
HINT: Perhaps you meant to reference the table alias "month_payments_clients".
: SELECT "clients".* FROM "clients" INNER JOIN "month_payments" "month_payments_clients" ON "month_payments_clients"."client_id" = "clients"."id" INNER JOIN (SELECT "clients"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("clients"."name"::text, '')) || to_tsvector('simple', coalesce("clients"."phone_number"::text, '')) || to_tsvector('simple', coalesce(pg_search_e49121e1954ad241e59d46.pg_search_b96d7c675f96dc5e635104::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_12c411d94675b7f72bc460::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_db73ee63dc62e95d223184::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a449af3665b9d7d8aaec7d::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a712b883f3cd839eb70c1e::text, ''))), (to_tsquery('simple', ''' ' || 'Sara' || ' ''' || ':*') || to_tsquery('simple', ''' ' || 'Connor' || ' ''' || ':*')), 0)) AS rank FROM "clients" LEFT OUTER JOIN (SELECT "clients"."id" AS id, string_agg("month_payments"."payment_note"::text, ' ') AS pg_search_b96d7c675f96dc5e635104 FROM "clients" INNER JOIN "month_payments" ON "month_payments"."client_id" = "clients"."id" GROUP BY "clients"."id") pg_search_e49121e1954ad241e59d46 ON pg_search_e49121e1954ad241e59d46.id = "clients"."id" LEFT OUTER JOIN (SELECT "clients"."id" AS id, string_agg("addresses"."address_1"::text, ' ') AS pg_search_12c411d94675b7f72bc460, string_agg("addresses"."town"::text, ' ') AS pg_search_db73ee63dc62e95d223184, string_agg("addresses"."state"::text, ' ') AS pg_search_a449af3665b9d7d8aaec7d, string_agg("addresses"."zip_code"::text, ' ') AS pg_search_a712b883f3cd839eb70c1e FROM "clients" INNER JOIN "addresses" ON "addresses"."client_id" = "clients"."id" GROUP BY "clients"."id") pg_search_859b556cd4a924628ee183 ON pg_search_859b556cd4a924628ee183.id = "clients"."id" WHERE ((to_tsvector('simple', coalesce("clients"."name"::text, '')) || to_tsvector('simple', coalesce("clients"."phone_number"::text, '')) || to_tsvector('simple', coalesce(pg_search_e49121e1954ad241e59d46.pg_search_b96d7c675f96dc5e635104::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_12c411d94675b7f72bc460::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_db73ee63dc62e95d223184::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a449af3665b9d7d8aaec7d::text, '')) || to_tsvector('simple', coalesce(pg_search_859b556cd4a924628ee183.pg_search_a712b883f3cd839eb70c1e::text, ''))) @@ (to_tsquery('simple', ''' ' || 'Sara' || ' ''' || ':*') || to_tsquery('simple', ''' ' || 'Connor' || ' ''' || ':*')))) AS pg_search_8f4d9088121acfab37f4a5 ON "clients"."id" = pg_search_8f4d9088121acfab37f4a5.pg_search_id WHERE "month_payments"."date" = $1 ORDER BY pg_search_8f4d9088121acfab37f4a5.rank DESC, "clients"."id" ASC LIMIT $2
has_many :month_payments, dependent: :destroy
pg_search_scope :search,
against: %i[name phone_number],
associated_against: {
month_payments: [:payment_note],
addresses: %i[address_1 town state zip_code]
},
using: { tsearch: { prefix: true, any_word: true } }
belongs_to :client