Reputation: 215
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} %> </td>
<td><%= c.shopping_list_name %> </td>
<td><%= c.shopping_list_status %> </td>
<td><%= c.total_items %> </td>
<td><%= link_to 'Edit', {:action => 'edit', :id => c.id} %> </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
Upvotes: 1
Views: 93
Reputation: 1138
If you really want this:
Select count(*) from products where shopping_list_id = '';
You can do:
Product.count(:conditions => "shopping_list_id is NULL")
Upvotes: 3