在我的postgres数据库上,我有一个使用UUID的主键。下面的样本设置
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
Run Code Online (Sandbox Code Playgroud)
当我尝试使用付款获取所有访问时,出现错误:
Visit.joins(:payments)
# => operator does not exist: character varying = uuid`
Run Code Online (Sandbox Code Playgroud)
基本上,这要求我显式地将visit.idto转换为to varchar,如果我的join语句是字符串,则可以通过以下方式轻松实现:
connections.connectable_id = visits.id::varchar
Run Code Online (Sandbox Code Playgroud)
但是我使用Arel的可组合性。
任何人都可以指导我如何直接使用Arel进行排版,因此我可以轻松地执行以下操作:
join(connections_table).on(connections_table[:connectable_id].eq(cast_to_string(visits_table[:id])))
# where connections_table and visits_table are Arel tables
Run Code Online (Sandbox Code Playgroud)