2013-02-26 42 views
1

我想在rails上实现ruby中的购物列表。我在数据库中创建了两个表,productsshopping_list。我已经为产品提供了一个购物清单的外键引用,它是product.shopping_list_id实现更新数据库中的数据导轨3.2时出错

我正试图找到目前在轨道上的红宝石shopping list,但我得到一个错误。

我创店模式如下: -

class CreateShopSchema < ActiveRecord::Migration 
    def up 

     # -------------------- 

     # Table Name: shopping_list 
     # -------------------- 
     # Note: This is the base table that has details about an account. 
     # --------------------------------------------------------------- 

    create_table :shopping_lists do |t| 
      t.string  :id, :null => false 
      t.string  :shopping_list_name 
      t.string  :shopping_list_status, :default => 'OPEN'   
      t.string  :total_items, :default => 0 
      t.timestamps 
    end 

     # -------------------- 
     # Table Name: products 
     # -------------------- 
     # Note: This is the base table that has details about an account. 
     # --------------------------------------------------------------- 

    create_table :products do |t| 
      t.references :shopping_list, :null => false 
      t.string  :product_name, :default => 'null' 
      t.string  :product_category 
      t.integer :quantity   
      t.string  :status, :default => 'OPEN' 
    end 


    end 

    def down 
    drop_table :products 
    drop_table :shopping_lists 
    end 

end 

我和shopping_list表如下: -

CREATE TABLE shopping_lists 
(
    id serial NOT NULL, 
    shopping_list_name character varying(255), 
    shopping_list_status character varying(255) DEFAULT 'OPEN'::character varying, 
    total_items character varying(255) DEFAULT 0, 
    created_at timestamp without time zone NOT NULL, 
    updated_at timestamp without time zone NOT NULL, 
    deleted integer NOT NULL DEFAULT 0, 
    CONSTRAINT shopping_lists_pkey PRIMARY KEY (id) 
) 

我的产品表如下: -

CREATE TABLE products 
(
    id serial NOT NULL, 
    shopping_list_id integer NOT NULL, 
    product_name character varying(255) DEFAULT 'null'::character varying, 
    product_category character varying(255), 
    quantity integer, 
    status character varying(255) DEFAULT 'OPEN'::character varying, 
    deleted integer NOT NULL DEFAULT 0, 
    CONSTRAINT products_pkey PRIMARY KEY (id) 
) 

我的产品控制器如下: -

class ProductsController < ApplicationController 
    # GET /products 
    # GET /products.json 
    def index 
    @products = Product.all 
    # @products = Product.includes(:shopping_list_id) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @products } 
    end 
    end 

    # GET /products/1 
    # GET /products/1.json 
    def show 
    @product = Product.find(params[:id]) 
    #product = Product.includes(:shopping_list_id) 
    #@product = Product.joins(:shopping_list_id) 


    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @product } 
    end 
    end 

    # GET /products/new 
    # GET /products/new.json 
    def new 
    @product = Product.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @product } 
    end 
    end 

    # GET /products/1/edit 
    def edit 
    @product = Product.find(params[:id]) 
    end 

    # POST /products 
    # POST /products.json 
    def create 
    @product = Product.new(params[:product]) 

    respond_to do |format| 
     if @product.save 
     format.html { redirect_to @product, notice: 'Product was successfully created.' } 
     format.json { render json: @product, status: :created, location: @product } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /products/1 
    # PUT /products/1.json 
    def update 
    @product = Product.find(params[:id]) 

    respond_to do |format| 
     if @product.update_attributes(params[:product]) 
     format.html { redirect_to @product, notice: 'Product was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 


    # DELETE /products/1 
    # DELETE /products/1.json 


def delete 
    @product = Product.find(params[:id]) 
    @product.deleted = 1 
    @product.save 

    respond_to do |format| 
     format.html { redirect_to products_url } 
     format.json { render :json => { :success => true } } 
    end 
    end 

    def list 
    @products = Product.find(:all, 
       :order => "id asc") 
    # @products = Product.includes(:shopping_list_id) 



    respond_to do |format| 
     format.html { render :action => 'list' } 
     format.json { render :json => @accounts.to_json } 
    end  
    end 

end 

当我试图通过前端在数据库中添加新产品时,产品表已填充,但shopping_list中的数量(总项目)没有在数据库中得到更新。

我想以这样的方式来实现,这样我可以得到特别的购物清单中的产品的数量

谁能帮我实现这个

回答

1

好了你为什么不考虑设置您的应用程序如下:

Product.rb

belongs_to :category 
belongs_to :shopping_list 


def total_price 
    product.price * quantity 
    end 

Category.rb

has_many :products 

LINE_ITEM

belongs_to :product 
belongs_to :shopping_list 
belongs_to :order --> will let you finish the rest 

shopping_list.rb

has_many :line_items 

您的订单表将包括quantity。要获得什么是你为了你会做类似下面的quantity

def quantity 
    line_items.to_a.sum { |quant| quant.quantity } 
    end 

把上述产品模型内部将意味着你将能够做你的product/index.html.erb - ><%= order.quantity %>

+0

我在哪里放 def quantity line_items.to_a.sum {| quant | quant.quantity} end in Product.rb? @Djj – Rockking 2013-02-26 05:44:17

+0

订单表中没有。实际上,您将拥有以下关系“产品”,“类别”,“line_item”,“shopping_list”和“订单”。 – David 2013-02-26 05:46:13

+0

但是我不能在我的应用程序中实现关系? 我将不得不从头开始@Djj – Rockking 2013-02-26 05:48:46