Sql 返回null的和
我对Ruby SQL查找函数有一个问题:它总是返回null 以下是终端输出:Sql 返回null的和,sql,ruby-on-rails,ruby,ruby-on-rails-3,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
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
你能分享其中的模型代码吗?从SQL中可以明显看出,多个ActiveRecords之间存在着某种关系,这将有助于了解。如果您选择point_store_items.point_cost并直接与数据库对话,则查询将返回什么?感谢各位的快速响应。。我编辑了这篇文章,把我认为与这个问题有关的模型包括在内。此外,我将很快让您知道查询返回的内容。@muistooshort如果我将self.count:point\u cost替换为select point\u store\u items.point\u cost它将在PointStore::pointstoreitems controllerbuy中返回NameError直接询问数据库,将Rails推开并查看数据。