3
私のpostgresデータベースでは、私はUUIDを使って主キーを持っています。Arelに結合列を型キャストする方法
Visit.joins(:payments)
# => operator does not exist: character varying = uuid`
基本的にこれは私が明示的に場合、私は簡単に行うことができた、varchar
にvisit.id
をキャストしている必要があります私は支払いを持つすべての訪問を取得しようとすると、私はエラーを得た
class Visit
# primary key id: uuid
has_many :connections, as: :connectable
has_many :payments, through: :connections
end
class Connection #(polymorphic class joining visit and payment)
# columns connectable_type(string), connectable_id(string)
belongs_to :payments
belongs_to :connectable, polymorphic: true
end
class Payment
# primary key id: uuid
has_many :connections
end
以下のサンプルのセットアップ私の結合ステートメントは以下のように文字列でした:
connections.connectable_id = visits.id::varchar
ただし、私はArelを合成に使用しています。私が直接ARELでこれを型キャストすることができますので、私は簡単のような何かをする可能性がどのようになど
でした誰ガイド:
はこれで遊んでいる間、私はARELNamedFunction
知った
join(connections_table).on(connections_table[:connectable_id].eq(cast_to_string(visits_table[:id])))
# where connections_table and visits_table are Arel tables