我是Ruby on Rails的新手,我在刷新/重新載入關係時遇到問題。在下面的代碼片段中,我試圖在'customer'對象上設置'store'屬性,然後讓customer對象返回嵌套store屬性,而不是它的id。有沒有一種方法可以設置商店屬性,然後告訴RoR「讓我到客戶並保存商店」而不做另一種急切的讀取負載?Ruby刷新對象
store_id = params[:id];
store = Store.find(store_id);
session[ :store_id ] = store_id;
customer_id = session[ :customer_id ];
customer = Customer.find(customer_id);
customer.store = store;
customer.save
respond_to do |format|
format.any(:json) { render :json => customer.to_json }
end
任何幫助或任何建議,不管它們是否與原始問題有關,是非常感謝。
所以你說'customer.store'只返回商店ID?它應該返回一個'Store'類對象。或者你問如何消除使用'customer.store'的額外數據庫調用? – iwasrobbed 2012-01-10 05:46:23