2016-05-26 10 views
1

Ich habe Modell Conversation, User, die durch has_many *, through: users_conversations verbunden sind.PG-Operator existiert nicht: Uuid = Text

Ich habe diesen Bereich:

class Conversation < ActiveRecord::Base 
    has_many :users_conversations 
    has_many :users, through: :users_conversations 

    scope :by_participants, ->(ids) { 
    joins(:users).where("users.id = ALL (ARRAY[?])", ids) 
    } 
end 

Es wird erwartet, Gespräche zu finden, in denen alle erforderlichen Benutzer (streng) teilnehmen.

Conversation.by_participants first 
    Failure/Error: its(:first) {is_expected.to eq(@conversation)} 

    ActiveRecord::StatementInvalid: 
     PG::UndefinedFunction: ERROR: operator does not exist: uuid = text 
     LINE 1: ...= "users_conversations"."user_id" WHERE (users.id = ALL (ARR... 
                    ^
     HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
     : SELECT "conversations".* FROM "conversations" INNER JOIN "users_conversations" ON "users_conversations"."conversation_id" = "conversations"."id" INNER JOIN "users" ON "users"."id" = "users_conversations"."user_id" WHERE (users.id = ALL (ARRAY['7c1a06c8-10c7-417f-96ea-c9c50fcaed35','8af2beca-d5f2-48d7-9857-7b5d124eaac1'])) ORDER BY "conversations"."id" ASC LIMIT 1 
    # ./spec/models/conversation_spec.rb:19:in `block (3 levels) in <top (required)>' 

Ich habe versucht, in ein Array zu injizieren cast (*) as uuid, aber PG scheint diese nur als Zeichenfolge zu beachten:

HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
     : SELECT "conversations".* FROM "conversations" INNER JOIN "users_conversations" ON "users_conversations"."conversation_id" = "conversations"."id" INNER JOIN "users" ON "users"."id" = "users_conversations"."user_id" WHERE (users.id = ALL (ARRAY['cast (6d4ff0b3-e148-40f8-87b5-d4d03577577a as uuid)','cast (432f4f6e-5832-4831-80dc-c747b8d3e742 as uuid)'])) 
+0

Haben Sie versucht, ANY ('Joins (: Benutzer) .where (" users.id = ANY (ARRAY [?]) "," Ids) ') anstelle von ALL? – imechemi

+0

@imechemi, ist es unwahrscheinlich, das Problem zu beheben und ist auch nicht das, was ich brauche –

Antwort

0

Haben Sie versucht das

class Conversation < ActiveRecord::Base 
    has_many :users_conversations 
    has_many :users, through: :users_conversations 

    scope :by_participants, ->(ids) { 
    joins(:users).where("users.id = ALL (ARRAY[?]::uuid[])", ids) 
    # it's all about ARRAY[?]::uuid[] type cast 
    } 
end 

Sie sind Zielspalte ist ::uuid[] aber Quelle ist text, einfach Cast eingeben und das Ergebnis sehen.