1

I have created two tables in the database, products and shopping_list. I have given products a foreign key reference of shopping_list which is product.shopping_list_id.

I am trying to find the total products present in the shopping list in ruby on rails but I am getting an error.

I have given a text box to enter the value,and that value is stored in the database as total items.

But when I add new products in the database the quantity is not getting updated in the database.

My shopping_list table is as follows:-

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)
)

My product table is as follows:-

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)
)

My Html document ie list in which I enter my value is this:-

<head>
  <%= javascript_include_tag :application %>
  <%= csrf_meta_tag %>
</head>
<% if @shopping_lists.blank? %>
  <p>There are no shopping_lists currently in the system.</p>
<% else %>
  <p>These are the shopping_lists in the system</p>
  <table border="1">
  <tr><td>shopping_list No.</td><td>shopping_list Name</td><td>status</td><td>quantity</td><td>Edit</td><td>Delete</td></tr>
  <% @shopping_lists.each do |c| %>
    <tr>
   <td><%= link_to c.id, {:action => 'get_product', :id => c.id} %> &nbsp;</td>
    <td><%= c.shopping_list_name %>&nbsp;</td>
    <td><%= c.shopping_list_status %>&nbsp;</td>
     <td><%= c.total_items %>&nbsp;</td>
    <td><%= link_to 'Edit', {:action => 'edit', :id => c.id} %> &nbsp;</td>
    <td><%= link_to 'Delete', {:action => 'delete', :id => c.id},
    :data => { :confirm => "Are you sure you want to delete this shopping_list?" } %></td>
    </tr>
  <% end %>
  </table>
<% end %>
<p><%= link_to "Add new shopping_list", {:action => 'new' }%></p>

In short I want to implement the following query:-

Select count(*) from products where shopping_list_id = '';

Can you please help me

1 Answer 1

3

If you really want this:

Select count(*) from products where shopping_list_id = '';

You can do:

Product.count(:conditions => "shopping_list_id is NULL")
Sign up to request clarification or add additional context in comments.

8 Comments

Using .size is better; if the Product collection already exists, it won't run a 2nd query against the database.
@ kaeros I wat to implement this in shopping list.so can you tell me where to make the changes
@Deefour Updated my answer, i think this reads better. What do you think?
@ArseneW @shopping_list.products.size In your case it is c.products.size
Where do I put this @Kaeros
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.