【发布时间】:2017-07-23 21:54:17
【问题描述】:
在我的 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
当我尝试通过付款获取所有访问时,出现错误:
Visit.joins(:payments)
# => operator does not exist: character varying = uuid`
基本上,这要求我将visit.id 显式转换为varchar,如果我的连接语句是字符串,我可以很容易地做到这一点:
connections.connectable_id = visits.id::varchar
但是我使用 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
【问题讨论】:
标签: ruby-on-rails ruby postgresql activerecord arel