Я подключил модели product
и cart_item
.
add_reference :cart_items, :product, null: false, foreign_key: true
schema.rb:
create_table "cart_items", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "product_id", null: false
t.bigint "cart_id", null: false
t.index ["cart_id"], name: "index_cart_items_on_cart_id"
t.index ["product_id"], name: "index_cart_items_on_product_id"
end
Но когда я пытаюсь создать и обратиться к атрибутам модели, я получаю ошибку:
undefined method `product' for #<CartItem:0x0000563873ac8a30>
Did you mean? product_id
a = CartItem.new
a.product_id = 5
a.cart_id = 1
a.save
a.product.title
#NoMethodError (undefined method `product' for #<CartItem:0x00007ff110f96b50>) Did you mean? product_id
a.product_id #=> 5