First of all, here's how to do what you want:
@user = current_user @shop = Shop.create(params[:shop]) @user.shop = @shop
Now why is your version not working:
You probably thought this might work, because if the User had a has_many relationship with Shop, @user.shops.create(params[:shop]) would work. However, there is a big difference between the has_many and has_one :
With a has_many relationship, shops returns an ActiveRecord collection object that has methods that you can use to add and remove stores / from the user. One of these methods is create , which creates a new store and adds it to the user.
With a has_one relationship has_one you are not returning such a collection object, but simply a Shop object owned by the user, or nil if the user does not already have a store. Since neither Shop objects nor nil have a create method, you cannot use create this way with has_one relationships.
sepp2k 01 Oct 2018-10-10 13:59
source share