【发布时间】:2011-09-13 23:45:52
【问题描述】:
我有以下一组模型:
class Cardstock < ActiveRecord::Base
has_many :color_matches, :primary_key => :hex, :foreign_key => :hex
has_many :palette_colors, :through => :color_matches
end
class ColorMatch < ActiveRecord::Base
belongs_to :palette_color
has_many :cardstocks, :foreign_key => :hex, :primary_key => :hex
end
class PaletteColor < ActiveRecord::Base
has_many :color_matches
has_many :cardstocks, :through => :color_matches
end
调用Cardstock.last.palette_colors 会产生以下错误:
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying = integer
LINE 1: ...".palette_color_id WHERE (("color_matches".hex = 66)) OR...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "palette_colors".* FROM "palette_colors" INNER JOIN "color_matches" ON "palette_colors".id = "color_matches".palette_color_id WHERE (("color_matches".hex = 66)) ORDER BY name ASC
这表明 ActiveRecord 生成的查询正在使用卡片的 id (66),而它应该使用卡片的十六进制 (bbbbaf)。在某处,我需要指定 ActiveRecord 使用hex 列连接cardstocks 和color_matches。 ActiveRecord 支持吗?
【问题讨论】:
-
顺便说一下,这是 Rails 2.3.x。
标签: sql ruby-on-rails activerecord has-many-through