Ruby on rails 为什么我能';你不喜欢关联的模型属性吗?
我连接了型号Ruby on rails 为什么我能';你不喜欢关联的模型属性吗?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我连接了型号产品和购物车项目 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:
产品
和购物车项目
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、 产品标识=5
a、 购物车id=1
a、 拯救
a、 产品名称
#NoMethodError(未定义的#的'product'方法)是什么意思?产品标识
a、 产品标识#=>5
根据您共享的信息,我相信您刚刚在模型类中添加了Db迁移,但没有添加ORM(ActiveRecord)关联
class CartItem
...
belongs_to :product #Hope you have a model named 'Product'
...
end
我相信添加上述行应该可以修复错误。您可以共享
CartItem
的代码吗?
class CartItem
...
belongs_to :product #Hope you have a model named 'Product'
...
end