Okay, so I am not able to update decimal value through the rails app (model class) but if changed from rails console it works perfectly fine.I am not able to save the updated record in to database this is my function def below
def self.currentprice_cal(id)
totalstock = @stockname.stocksinmarket+@stockname.stocksinexchange
@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
this my model class
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
in rails console it works fine
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
I dunno if I am doing something wrong here ,I am not able to figure it out
i am calling current price_cal from here
@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 => -@Buy_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)
Unless absolutely required, don't use instance variables in the class method that you've defined. There's a good chance that at least one of those instance variables isn't properly set when you call the method. Subsequently, your database row isn't updated.
Either pass the object and new value to the method as arguments, or pass the ID and values as arguments and fetch the object from the database within the method.