I want to use callbacks to modify the admin product controller to create a listing for each user. Using decorator in spree product model I added the "belongs_to :user" relation. In my custom user model I added the "has_many :products" relation and also added product_id and index product_id to my user data table. I have this:
Spree::Admin::ProductsController.class_eval do
create.before :user_products
private
def user_products
@user.objects.build params[object_name]
end
end
object_name is a function inherited from ResourceController. It returns a string containing current object name “product”.
But it is not working. I am getting "undefined method objects for User XXXXX" Looks like my association is not working. What am I doing wrong? Thanks in advance.
Your foreign key is wrong it should be like this
class User < ActiveRecord::Base
has_many :products
end
Class Product < ActiveRecord::Base
belongs_to :user
end
Products table should have user_id
here is reference http://guides.rubyonrails.org/association_basics.html#the-has-many-association