Rails模型的自定义关联

时间:2015-08-19 19:39:58

标签: ruby-on-rails activerecord

我正在尝试定义两个模型CustomerCustomerProduct之间的关系,并在customer.custom_id上关联customerproduct.customer_id,以便mycustomer.customer_products.create()以{{{{}}结束1}}而不是customerproduct.customer_id = customer.custom_id

这是我的客户模型:

customer.id

我的CustomerProduct模型:

class Customer < ActiveRecord::Base
  belongs_to :organisation
  has_many :customer_products, foreign_key: 'customer_id'
end

我不确定如何定义class CustomerProduct < ActiveRecord::Base belongs_to :customer end 位 - 我尝试将其添加为custom_id中的外键但这似乎没有做任何事情--Rails仍然需要{{1} }。

提前致谢!

1 个答案:

答案 0 :(得分:1)

我们只是说答案非常明显,所以我一定是盲目的!

has_many :customer_products, primary_key: 'custom_id', foreign_key: 'customer_id'
相关问题