Ho la seguente serie di modelli:Rails has_many: attraverso con personalizzato 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
Calling Cardstock.last.palette_colors
produce il seguente errore:
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
Questo mi dimostra che la query ActiveRecord genera è utilizzare il id del cartoncino (66
) dove dovrebbe essere utilizzato l'esagono del cartoncino (bbbbaf
). Da qualche parte, ho bisogno di specificare ActiveRecord per utilizzare la colonna hex
per unire tra cardstocks
e color_matches
. ActiveRecord supporta questo?
Questo è Rails 2.3.x, a proposito. –