我有这个安装在我的应用程序:rails4 ActiveRecord关联
User has_many :products
User has_many :product_leads, through: :products
Product belongs_to :user # (in products table there is user_id)
ProductLead belongs_to :product # (in product_leads table there is no user_id)
ProductLead belongs_to :user
在此设置user.product_leads
是工作,但product_lead.user
不是。所以我删除了最后一行。
我没有user_id
在product_leads
表中,但我甚至不想。 ProductLead表单填充为嵌套属性和Product表单。
有没有比下面更好的定义product_lead.user
的方法?或者我应该在product_leads
表中有user_id
?
product_lead.rb
def user
product.user
end
这就需要换入'ProductLead'了'product_id'外键的任务改为'products'表中的'product_lead_id',但这个障碍放在一边,它应该工作。 –