データベースに products と shopping_list という 2 つのテーブルを作成しました。product.shopping_list_id である shopping_list の外部キー参照を製品に与えました。
ruby on rails で買い物リストにある商品の合計を見つけようとしていますが、エラーが発生します。
値を入力するためのテキスト ボックスを用意しました。その値は合計アイテムとしてデータベースに格納されます。
しかし、データベースに新製品を追加しても、データベースで数量が更新されません。
私の 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)
)
私のHtmlドキュメント、つまり値を入力するリストは次のとおりです:-
<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>
要するに、次のクエリを実装したいと思います:-
Select count(*) from products where shopping_list_id = '';
手伝ってくれませんか