DavyJonesLocker/postgres_ext-serializers

PG::UndefinedParameter: ERROR: there is no parameter $1

jyr opened this issue · 7 comments

jyr commented

Hi!

I have of next versions:

  • Rails 4.1.1
  • postgres_ext (2.4.1)
  • postgres_ext-serializers (0.0.3)
  • active_model_serializers (0.8.3)

my query is:

@model1.model2.includes(:model3).where('model3.state =?', 'done')

I have a error when run:

json = ActiveModel::ArraySerializer.new(ts, each_serializer: Api::V1::MySerializer).to_json

or

json = @model1.model2.includes(:model3).where('model3.state =?', 'done')
render json: json, each_serializer: Api::V1::MySerializer

Could you help me?

Thanks a lot!

Please try with master, I think this is already fixed.

jyr commented

@felixbuenemann Hi,

I tried with:

gem 'postgres_ext-serializers', git: 'https://github.com/DockYard/postgres_ext-serializers.git'

the new version is:

postgres_ext-serializers (0.0.3 5de27f2)

I have the same error:

PG::UndefinedParameter: ERROR:  there is no parameter $1
LINE 1: ..." = "table1"."id" WHERE "table1"."attribute_id" = $1 AND (bo...

It's hard to say with all the anonymised queries, but have you tried this?

@model1.model2.joins(:model3).where("model3.state = 'done'")

You don't really need a prepared statement for a static string.

Another possible solution using a subquery:

model3_ids = Model3.where(state: 'done').select(:id)
@model1.model2.where(model3_id: model3_ids)

The subquery approach is often faster than a join.

jyr commented

Not works, I have the same error:

ActiveRecord::StatementInvalid (PG::UndefinedParameter: ERROR: there is no parameter $1

I think is error is of rails 4, the sql result is:

LINE 1: ..." = "model3"."id" WHERE "model3"."model1_id" = $1 AND "ti

Can you post the result of calling to_sql on your relation?

It looks like you have some code that is trying to do preloading and none of the examples I posted above would trigger that.

jyr commented

the result of sql:

"SELECT \"tickets\".* FROM \"tickets\" INNER JOIN \"bookings\" ON \"tickets\".\"booking_id\" = \"bookings\".\"id\" WHERE \"bookings\".\"event_id\" = $1 AND (bookings.state ='complete')"

and query

event.tickets.includes(:booking).where('bookings.state =?', 'done')

and the result of json query when I used a serializer

(WITH tickets_attributes_filter AS (SELECT "tickets"."number", "tickets"."consecutive_number", "tickets"."ticket_type_name", "tickets"."email", "tickets"."first_name", "tickets"."last_name", "tickets"."checked_in" FROM "tickets" INNER JOIN "bookings" ON "tickets"."booking_id" = "bookings"."id" WHERE "bookings"."event_id" = $1 AND (booking_id in (SELECT "bookings"."id" FROM "bookings" WHERE "bookings"."state" = 'complete') and event_id =2173)), tickets_as_json_array AS (SELECT COALESCE(json_agg(tbl), '[]') as tickets, 1 as match FROM (SELECT * FROM "tickets_attributes_filter") AS tbl), jsons AS (SELECT "tickets_as_json_array"."tickets" FROM "tickets_as_json_array") SELECT row_to_json(jsons) FROM "jsons")

Ah, that should be easy to fix. Try Ticket.where(event: event).…