简体   繁体   English

Rails has_many:through with custom foreign_key

[英]Rails has_many :through with custom foreign_key

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:调用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

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 ).这表明 ActiveRecord 生成的查询正在使用卡片的 id ( 66 ),它应该使用卡片的十六进制 ( bbbbaf )。 Somewhere, I need to specify to ActiveRecord to use the hex column to join between cardstocks and color_matches .在某处,我需要指定 ActiveRecord 使用hex列连接cardstockscolor_matches Does ActiveRecord support this? ActiveRecord 是否支持这个?

Your relationships are all out of whack here.你的人际关系在这里完全不正常。

  • the relationships between Cardstock and ColorMatch should be a has_and_belongs_to_many relationship on both sides Cardstock 和 ColorMatch 之间的关系应该是双方的has_and_belongs_to_many关系
  • anywhere you have a has_many relationship , you need a corresponding belongs_to relationship in the corresponding class任何有has_many relationship的地方,都需要在对应的 class 中有对应的belongs_to关系

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.弄清楚多对多的两侧是什么,连接 model 是什么。 I'm going to give an example assuming that ColorMatch is your join model -- it's what relates a PaletteColor to a Cardstock.我将举一个例子,假设 ColorMatch 是您的加入 model - 它是将 PaletteColor 与卡片纸相关联的原因。 In that case, you'll want your relationships to look something like this:在这种情况下,你会希望你的关系看起来像这样:

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
  belongs_to :cardstocks, :foreign_key => :hex, :primary_key => :hex
end

class PaletteColor < ActiveRecord::Base
  has_many :color_matches
  has_many :cardstocks, :through => :color_matches
end

In terms of your database, you should have a palette_color_id and a hex field on the color_matches table, and a hex field on the cardstocks table.就您的数据库而言,您应该在color_matches表上有一个palette_color_id和一个hex字段,在cardstocks表上有一个hex字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM