小编Ale*_*ahn的帖子

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
Run Code Online (Sandbox Code Playgroud)

调用会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 …
Run Code Online (Sandbox Code Playgroud)

sql activerecord ruby-on-rails has-many-through

5
推荐指数
1
解决办法
2339
查看次数