繁体   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