這是有史以來最紅的事情發生在我與紅寶石/鐵軌。對象沒有被加載
我有模特,店鋪,其中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
嘗試在查詢之前調試對象 –