0

我在使用 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
4

0 回答 0