总和返回null

时间:2013-05-25 02:17:19

标签: sql ruby-on-rails ruby ruby-on-rails-3

我遇到了Ruby SQL查找功能的问题:它总是返回null。

这是终端输出:

SELECT COUNT("point_store_items"."point_cost") 
FROM "point_store_items" 
INNER JOIN "point_store_items_point_store_orders" 
ON "point_store_items"."id" = "point_store_items_point_store_orders"."point_store_item_id" 
WHERE "point_store_items_point_store_orders"."point_store_order_id" IS NULL

导致此问题的代码是:

def self.pointcost
  self.count(:point_cost)
end

如果此代码更改为

def self.pointcost
  40
end

它没有问题,除了总是列出的价格是40,而不是应该来自数据库。

相关型号代码:

class PointStoreItem < ActiveRecord::Base
  attr_accessible :product_id, :point_cost
  has_and_belongs_to_many :orders, :class_name => "PointStoreOrder"
  has_many :users, :through => :orders
  belongs_to :product
  def to_param
    "#{id}-#{product.name.parameterize}"
  end
  def self.pointcost
        self.count(:point_cost)
  end
end

要求成本点

的模型
class PointStoreOrder < ActiveRecord::Base
  include Workflow
  attr_accessible :point_transaction_id, :user_id, :workflow_state ,          :shipping_provider, :tracking_number, :items
  has_and_belongs_to_many :items, :class_name => "PointStoreItem"
  belongs_to :user
  belongs_to :point_transaction
  accepts_nested_attributes_for :items
  workflow do
    state :cart do
      event :purchase, :transitions_to => :purchased
      event :cancel, :transitions_to => :cancelled
    end
    state :purchased do
      event :ship, :transitions_to => :shipped
    end
    state :shipped
    state :cancelled
  end
  def purchase
    unless self.user.point_balance >= total
      halt "Insufficient point balance"
    else
      self.point_transaction = user.point_transactions.new
      self.point_transaction.point_count = total
      self.point_transaction.save!
      self.save!
    end
  end
  def total
    self.items.pointcost
  end
  def on_cancelled_entry(old_state, event, *args)
  end
end

0 个答案:

没有答案