bartgras
bartgras

Reputation: 442

Correct user model setup for spree

I'm trying to setup spree together with devise (not 'spree_auth_devise' gem but standalone devise). I've followed http://guides.spreecommerce.com/developer/authentication.html guide and everything is working well until in a shop I click "Add To Cart" which brings following error:

NoMethodError in Spree::OrdersController#edit
undefined method `orders' for #<User:0x007f6bb8782730>

I can fix that error by adding to User model:

def orders
   spree_orders
end

but I assume that's not the way it should be fixed.

Can anyone tell me the right way of setting it up so that I don't get that error?

Upvotes: 0

Views: 452

Answers (3)

Abram
Abram

Reputation: 41884

The correct way is to add this to your user model:

has_many :orders, class_name: 'Spree::Order'

https://github.com/spree/spree/issues/5393#issuecomment-56445173

Upvotes: 0

Arvind singh
Arvind singh

Reputation: 1392

There should be a proper association between User and Order. User can have multiple orders so update the code as given below -

class User < ActiveRecord::Base
   has_many :orders
end


class Order < ActiveRecord::Base
  belongs_to :user
end

Upvotes: 0

Hates_
Hates_

Reputation: 68681

Adding the orders association would be the right way to correct the error:

has_many :orders, foreign_key: :user_id

If you look at LegacyUser in the Spree project that will show a lot of the barebones that your user model needs to provide to be able to function properly with Spree.

https://github.com/spree/spree/blob/master/core/app/models/spree/legacy_user.rb

Upvotes: 1

Related Questions