这是有史以来最红的事情发生在我与红宝石/铁轨。对象没有被加载
我有模特,店铺,其中has_many 余额。而且我有一种方法可以根据商店的货币为我提供默认余额。
商店模型。
class Store < ActiveRecord::Base
has_many :balances, as: :balanceable, dependent: :destroy
def default_balance
#puts self.inspect <- weird part.
balances.where(currency: self.currency)[0]
end
...
end
平衡模型。
class Balance < ActiveRecord::Base
belongs_to :balanceable, :polymorphic => true
...
end
好了,那么在均衡控制器我有显示行动,这会给我一个特定的余额或默认的。
平衡控制器。
class Api::Stores::BalancesController < Api::Stores::BaseController
before_filter :load_store
# Returns a specific alert
# +URL+:: GET /api/stores/:store_id/balances/:id
def show
#puts @store.inspect <- weird part.
@balance = (params[:id] == "default") ? @store.default_balance : Balance.find(params[:id])
respond_with @balance, :api_template => :default
end
...
private
# Provides a shortcut to access the current store
def load_store
@store = Store.find(params[:store_id])
authorize! :manage, @store
end
end
现在这里是怪异的一部分来... ...
如果我做的演出行动的呼吁;例如:
GET/API /专卖店/ 148 /余额/默认
它返回null(因为货币被设置为空,且有与空的货币无余额),以及生成的SQL查询是:
SELECT `balances`.* FROM `balances` WHERE `balances`.`balanceable_id` = 148 AND `balances`.`balanceable_type` = 'Store' AND `balances`.`currency` IS NULL
,所以我不知道为什么...它是设置货币为NULL。 但如果在这个过程中的任何地方,我把
提出@ store.inspect
或default_balance
方法中:
提出self.inspect
它神奇的作品!
所以我不知道为什么会发生这种情况......看起来像商店对象没有加载,直到我“检查”它或类似的东西。
谢谢
“货币”是表格专卖店的一列吗? – Yanhao
是的。 '货币:字符串(255)' – esbanarango
尝试在查询之前调试对象 –