2021年1月28日星期四

Has_Many_Through Failing To Push a Many to a Through

I'm trying to set up a has_many_through relationship where a User can have a cart with an item. I can add carts to the user (chip.carts << chips_cart) but I can't push items into my carts (chips_cart << coffee).

I get NoMethodError: undefined method <<' for #<Cart id: 5, user_id: nil>`

class User < ActiveRecord::Base      has_many :carts      has_many :items, through: :carts  end  class Cart < ActiveRecord::Base      belongs_to :user      has_many :items  end  class Item < ActiveRecord::Base      belongs_to :carts      has_many :users, through: :carts  end    chip = User.create(username: "Chip")  chips_cart = Cart.create  socks = Item.create(item: "socks", price: 5.00)  
https://stackoverflow.com/questions/65947450/has-many-through-failing-to-push-a-many-to-a-through January 29, 2021 at 09:09AM

没有评论:

发表评论