gpt4 book ai didi

ruby-on-rails - 在 Rails 3.2 中实现计数时出错

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:59 25 4
gpt4 key购买 nike

我在数据库中创建了两个表,products 和 shopping_list。我已经为产品提供了 shopping_list 的外键引用,即 product.shopping_list_id。

我试图在 ruby​​ on rails 中查找购物 list 中的所有产品,但出现错误。

我给了一个文本框来输入值,该值作为总项目存储在数据库中。

但是当我在数据库中添加新产品时,数据库中的数量没有更新。

我的 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 文档 ie 列表是这样的:-

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

简而言之,我想实现以下查询:-

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

你能帮帮我吗

最佳答案

如果你真的想要这个:

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

你可以这样做:

Product.count(:conditions => "shopping_list_id is NULL")

关于ruby-on-rails - 在 Rails 3.2 中实现计数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com