簡體   English   中英

將表連接到購物車

[英]Connecting tables to shopping cart

將表連接到購物車

我有三個模型和三個數據庫表,我想連接到一個購物車,我是新的rails並且有一些問題要做到這一點。 我最初的想法是創建模型,稱為服務作為模型廣告,Package_of_products和訂閱的父級。 然后通過Line_item將其連接到購物車已經知道我做錯了每次嘗試將我的一項服務添加到Line_items時我收到消息

ActiveRecord::RecordNotFound in LineItemsController#create

Couldn't find Service without an ID

app/controllers/line_items_controller.rb:44:in `create'

我已經有了

def create
  @cart = current_cart
  service = Service.find(params[:service_id])
  @line_item = @cart.line_items.build(:service => service)

respond_to do |format|
  if @line_item.save
    format.html { redirect_to(@line_item.cart, :notice => 'Line item was successfully created.')   
end

我有4個數據庫和模型我的Line_items

class LineItem < ActiveRecord::Base
belongs_to :service
belongs_to :cart
end

大車

class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
has_many :services,
has_many :adverts, :through => :services
has_many :package_of_products, :through => :services
has_many :subscriptions,:through => :services

廣告

class Advert < ActiveRecord::Base
  belongs_to :service
end

訂閱

class Subscription < ActiveRecord::Base
  belongs_to :service
end

Package_of_products

class PackageOfProduct < ActiveRecord::Base
  belongs_to :service
end

好吧,首先聯想的名字是belongs_to ,而不是belong_to ,所以請糾正錯字。

然后我認為你需要這樣的smth:

class Cart  < ActiveRecord ::Base
  has_many :line_items, :dependant => destroy
  has_many :ads, :through => :line_items
  has_many :products, :through => :line_items
  has_many :services, :through => :line_items
end

has_many :through 這里的關聯檢查has_many :through

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM