Ruby on rails Rails模型的自定义关联
我试图定义两个模型Ruby on rails Rails模型的自定义关联,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我试图定义两个模型Customer和CustomerProduct之间的关系,在CustomerProduct.Customer\u id上链接Customer.Customer\u-products.create()以customerproduct.customer\u id=customer.custom\u id结束,而不是customer.id 这是我的客户模型: class Customer < ActiveRecord::Base belongs_to :organisat
Customer
和CustomerProduct
之间的关系,在CustomerProduct.Customer\u id
上链接Customer.Customer\u-products.create()
以customerproduct.customer\u id=customer.custom\u id
结束,而不是customer.id
这是我的客户模型:
class Customer < ActiveRecord::Base
belongs_to :organisation
has_many :customer_products, foreign_key: 'customer_id'
end
class客户
以及我的客户产品型号:
class CustomerProduct < ActiveRecord::Base
belongs_to :customer
end
class CustomerProduct
我不确定如何定义custom\u id
位-我尝试将其作为外键添加到belowns\u to:customer
中,但这似乎没有任何作用-Rails仍然使用customer.id
提前谢谢 我们就说答案很明显,所以我一定是瞎了
has_many :customer_products, primary_key: 'custom_id', foreign_key: 'customer_id'
如果customer\u id
是主键,但您没有说它在您的问题中,则这是正确的。因此,我先前的评论是基于customer\u id
只是“另一个属性”。