在Rails 3.2中实现计数时出错

时间:2013-02-25 12:43:03

标签: ruby-on-rails database ruby-on-rails-3 html5 postgresql

我在数据库,products和shopping_list中创建了两个表。我已经为product_list提供了product_shopping_list_id的外键引用。

我正在尝试在轨道上的红宝石中找到购物清单中的总产品,但我收到了错误。

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

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

我的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} %> &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 = '';

你能帮我吗

1 个答案:

答案 0 :(得分:3)

如果你真的想要这个:

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

你可以这样做:

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