Rails has_many :通过自定义foreign_key
我有以下一组模型:
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 支持这个吗?
I have the following set of models:
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
Calling Cardstock.last.palette_colors
yields the following error:
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
This shows me that the query ActiveRecord generates is using the cardstock's id (66
) where it should be using the cardstock's hex (bbbbaf
). Somewhere, I need to specify to ActiveRecord to use the hex
column to join between cardstocks
and color_matches
. Does ActiveRecord support this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你们的关系在这里完全不正常。
has_many 关系
的地方都应该是has_and_belongs_to_many
关系,您需要在对应班级Your relationships are all out of whack here.
has_and_belongs_to_many
relationship on both sideshas_many relationship
, you need a correspondingbelongs_to
relationship in the corresponding class你们建立关系的方式有问题。我不太明白你这里的具体用例,所以我不确定问题出在哪里。思考这个问题的方式可能是多对多关系。弄清楚多对多的两侧是什么,以及连接模型是什么。我将举一个例子,假设 ColorMatch 是您的连接模型——它将 PaletteColor 与 Cardstock 关联起来。在这种情况下,您将希望您的关系看起来像这样:
就您的数据库而言,您应该在
上有一个
表和palette_color_id
和一个hex
字段color_matchescardstocks
表上的hex
字段。There's something wrong with the way your relationships are set up. I don't quite understand your specific use case here, so I'm not sure where the problem is. The way to think about this is probably as a many-to-many relationship. Figure out what the two sides of that many-to-many are, and what's the join model. I'm going to give an example assuming that ColorMatch is your join model -- it's what relates a PaletteColor to a Cardstock. In that case, you'll want your relationships to look something like this:
In terms of your database, you should have a
palette_color_id
and ahex
field on thecolor_matches
table, and ahex
field on thecardstocks
table.