ActiveRecord :has_many and custom foreign_key-Collection of common programming errors

I have the following structure:

class Product < ActiveRecord::Base
    has_many :product_units
    has_many :units, :through => :product_units, :foreign_key => :unitID
end

class Unit < ActiveRecord::Base
    has_many :product_units
    has_many :products, :through => :product_units, :foreign_key => :productID
end

class ProductUnit < ActiveRecord::Base
    belongs_to :unit, :foreign_key => :unitID
    belongs_to :product, :foreign_key => :productID
end

but as I try to run

Product.last.units.create({:name => 'test', :symbol => 't'})

The following error occurs :

ActiveRecord::UnknownAttributeError: unknown attribute: product_id

Can someone help me ? it’s driving me crazy 😡

EDIT: When I try the following:

Product.last.units

I get the error

SELECT "units".* FROM "units" INNER JOIN "product_units" ON "units"."unitID" = "product_units"."unitID" WHERE "product_units"."product_id" = 1

The problem is that product_units has no product_id field, instead its name is productID; why the identifier name is correct on join but not in the where statement ?

Originally posted 2013-11-10 00:11:51.