2014-12-19 58 views
-1

好吧,所以我不能通過rails應用程序更新十進制值(模型類),但如果從rails控制檯更改,它的工作原理非常好。我無法保存更新記錄在數據庫 這低於十進制值沒有從rails應用程序更新,但從控制檯更改後得到更新

def self.currentprice_cal(id) 
    totalstock = @[email protected] 
    @stockname.currentprice = @Buy_id.price.to_f*@Buy_id.numofstock.to_f 
    @stockname.save 
    #@stockname.update(currentprice: @stockname.currentprice.to_f) 
    @update_currentprice_files = Stock.update_current_price(id,@stockname.currentprice) 
end 

我的功能高清這是我的模型類

class CreateStocks < ActiveRecord::Migration 
def change 
create_table :stocks do |t| 
    t.string :stockname 
    t.decimal :currentprice, precision: 4, scale: 2 
    t.decimal :dayhigh, precision: 4, scale: 2 
    t.decimal :daylow, precision: 4, scale: 2 
    t.decimal :alltimehigh, precision: 4, scale: 2 
    t.decimal :alltimelow, precision: 4, scale: 2 
    t.integer :stocksinexchange 
    t.integer :stocksinmarket 
    t.timestamps 
    end 
end 
end 

在軌安慰它工作正常

irb(main):015:0> u = Stock.first 
Stock Load (0.5ms) SELECT "stocks".* FROM "stocks" ORDER BY "stocks"."id" ASC LIMIT 1 
=> #<Stock id: 30677878, stockname: "Intel", currentprice: #  <BigDecimal:5fbdbf0,'0.4552E2',18(45)>, dayhigh: #<BigDecimal:5fbd790,'0.552E2',18(45)>, daylow: #<BigDecimal:5fbd3d0,'0.2201E2',18(45)>, alltimehigh: #<BigDecimal:5fbd100,'0.457E2',18(45)>, alltimelow: #<BigDecimal:5fbca70,'0.2209E2',18(45)>, stocksinexchange: 47, stocksinmarket: 3, created_at: "2014-12-18 06:50:08", updated_at: "2014-12-19 06:04:18"> 
irb(main):016:0> u.currentprice 
=> #<BigDecimal:5fbdbf0,'0.4552E2',18(45)> 
irb(main):017:0> u.currentprice = 45.34 
=> 45.34 
irb(main):018:0> u.save 
(0.2ms) begin transaction 
SQL (0.5ms) UPDATE "stocks" SET "currentprice" = ?, "updated_at" = ? WHERE "stocks"."id" = 30677878 [["currentprice", 45.34], ["updated_at", "2014-12-19 07:18:34.214567"]] 
(148.2ms) commit transaction 
=> true 

我不知道如果我在這裏做得不對,我無法弄清楚

我從這裏

 @user_buying = User.find(@Buy_id.user_id) 
     @user_buying.cash = @user_buying.cash - @Buy_id.price*@Buy_id.numofstock.to_f 
     logger.info @Buy_id.user_id 
     @user_buying.save 
     #@user_selling = User.select('cash').where(:id => @Sell_id.user_id).first 
     @user_selling = User.find(@Sell_id.user_id) 
     @user_selling.cash = @user_selling.cash + @Sell_id.priceexpected*@Buy_id.numofstock.to_f 
     @user_selling.save 

     @stockused = StockUsed.create(:user_id => @Buy_id.user_id, :stock_id => @Buy_id.stock_id,:numofstock => @Buy_id.numofstock) 
     @stockused = StockUsed.create(:user_id => @Sell_id.user_id, :stock_id => @Sell_id.stock_id,:numofstock => [email protected]_id.numofstock) 

     @stockname = Stock.select('stockname,stocksinmarket,stocksinexchange,currentprice').where('id'=>id).first 
     User.currentprice_cal(id) 

     @notification = Notification.create(:user_id =>@Buy_id.user_id, :notification => "You bought #{@Buy_id.numofstock} stocks of #{@stockname.stockname} at the rate of $#{@Buy_id.price} per share", :seen => 1, :notice_type => 1) 
     @notification = Notification.create(:user_id =>@Sell_id.user_id, :notification => "You sold #{@Buy_id.numofstock} stocks of #{@stockname.stockname} at the rate of $#{@Sell_id.priceexpected} per share", :seen => 1, :notice_type => 1) 
+0

發佈您調用'currentprice_cal'方法的代碼。 – roob 2014-12-19 07:50:23

+0

你從不使用你設置的'totalstock'變量?你在哪裏設置'@ stockname'?通過調用模型方法生成的是什麼sql? – Albin 2014-12-19 09:06:10

+0

可能是另一個[strong-parameters-induced confusion](http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html)...請發佈控制器代碼,作爲@roob詢問 – 2014-12-19 16:35:32

回答

1

除非絕對需要調用當前price_cal,不要使用實例您定義的類方法中的變量。調用方法時,至少有一個實例變量未正確設置的可能性很大。隨後,您的數據庫行不會更新。

將對象和新值作爲參數傳遞給方法,或者傳遞ID和值作爲參數並從方法中的數據庫中獲取對象。

+0

不能立即變量被稱爲類內的任何地方? – mojo1643 2014-12-23 16:03:46

+0

您正在使用類方法。我不知道你是否在類級別定義實例變量。如果你確定你做得對,那就不會有問題。 – Humza 2014-12-23 16:05:31

+0

mmmm讓我檢查我能夠訪問所有的變量,雖然只有保存不工作 – mojo1643 2014-12-23 16:20:32