2016-11-23 74 views
1

我想使用回调修改管理产品控制器以为每个用户创建列表。在spree产品模型中使用装饰器,我添加了“belongs_to:user”关系。在我的自定义用户模型中,我添加了“has_many:products”关系,并且还将product_id和index product_id添加到了我的用户数据表中。 我有这样的:Rails 4 Spree调用回调“用户xxxxx的未定义方法”

Spree::Admin::ProductsController.class_eval do 
create.before :user_products 

private 

def user_products 
    @user.objects.build params[object_name] 
end 
end 

object_name是从ResourceController继承的功能。它返回一个包含当前对象名称“product”的字符串。

但它不工作。我得到“用户XXXXX未定义的方法对象” 看起来像我的协会不工作。我究竟做错了什么?提前致谢。

+0

什么是你的自定义模型的名称,与用户和产品的关系? –

+0

我的自定义模型称为用户。 在用户模式,我有: 的has_many产品,CLASS_NAME: '狂欢::产品' 在施普雷产品型号,我有: 如果Spree.user_class belongs_to的:用户,CLASS_NAME:Spree.user_class.to_s 其他 belongs_to的:用户 – aminhs

+0

是你的关联是错误的user_id应该在spree_products表中,而不是用户表中的product_id –

回答

0

你的外键错了,它应该是这样的

class User < ActiveRecord::Base 
    has_many :products 
end 

Class Product < ActiveRecord::Base 
    belongs_to :user 
end 

产品表应该有user_id说明

这里参考http://guides.rubyonrails.org/association_basics.html#the-has-many-association

enter image description here

+0

感谢祝愿你的帮助。有用。 – aminhs

+0

欢迎@aminhs –

相关问题