title: Error while implementing update of data in database for rails 3.2
body: <p>I am trying to implement a shopping list in ruby on rails.I have created two tables in the database, <code>products</code> and <code>shopping_list</code>. I have given products a foreign key reference of shopping_list which is <code>product.shopping_list_id</code>.</p>
  
  <p>I am trying to find the <code>total products</code> present in the <code>shopping list</code> in ruby on rails but I am getting an error.</p>
  
  <p>My create Shop schema is as follows:-</p>
  
  <pre><code>class CreateShopSchema &lt; 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 =&gt; false
              t.string     :shopping_list_name
              t.string     :shopping_list_status, :default =&gt; 'OPEN'          
              t.string     :total_items, :default =&gt; 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 =&gt; false
              t.string     :product_name,  :default =&gt; 'null'
              t.string     :product_category
              t.integer    :quantity          
              t.string     :status, :default =&gt; 'OPEN' 
      end
  
  
    end
  
    def down
      drop_table :products
      drop_table :shopping_lists
    end
  
  end
  </code></pre>
  
  <p>My shopping_list table is as follows:-</p>
  
  <pre><code>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)
  )
  </code></pre>
  
  <p>My product table is as follows:-</p>
  
  <pre><code>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)
  )
  </code></pre>
  
  <p>My product controller is as follows:-</p>
  
  <pre><code>class ProductsController &lt; 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 =&gt; { :success =&gt; true } }
      end
    end
  
    def list
     @products = Product.find(:all,
                   :order =&gt; "id asc")
    # @products = Product.includes(:shopping_list_id)
  
  
  
      respond_to do |format|
        format.html { render :action =&gt; 'list' }
        format.json { render :json =&gt; @accounts.to_json }
      end       
    end
  
   end
  </code></pre>
  
  <p>When I am trying to add new products in the database via the front end ,the product table is populated but the quantity(total items) in shopping_list is not getting updated in the database.</p>
  
  <p>I want to implement in such a way so that I can get the count of the products of the particular shopping list</p>
  
  <p>Can anyone please help me implement this</p>