【问题标题】:decimal value not getting updated from the rails app but gets updated when changed from console十进制值未从 Rails 应用程序更新,但在从控制台更改时更新
【发布时间】:2014-12-19 07:24:48
【问题描述】:

好的,所以我无法通过 rails 应用程序(模型类)更新十进制值,但如果从 rails 控制台更改它可以正常工作。我无法将更新的记录保存到数据库中 这是我下面的函数定义

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

这是我的模型类

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

在 Rails 控制台中它工作正常

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

我不知道我是否在这里做错了什么,我无法弄清楚

我从这里调用当前 price_cal

       @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)

【问题讨论】:

  • 贴出你调用currentprice_cal方法的代码。
  • 您从不使用您设置的totalstock 变量?你在哪里设置@stockname?调用model方法生成什么sql?
  • 可能是另一个strong-parameters-induced confusion ...请按照@roob的要求发布控制器代码
  • 我正在调用 user.rb 模型类中的所有函数
  • 抱歉回复晚了 :p

标签: ruby-on-rails ruby activerecord sqlite bigdecimal


【解决方案1】:

除非绝对需要,否则不要在您定义的类方法中使用实例变量。当您调用该方法时,很有可能至少有一个实例变量没有正确设置。随后,您的数据库行不会更新。

要么将对象和新值作为参数传递给方法,要么将 ID 和值作为参数传递并在方法内从数据库中获取对象。

【讨论】:

  • 不能在类的任何地方调用即时变量吗?
  • 您正在使用类方法。而且我不知道您是否在类级别定义实例变量。如果您确定自己做对了,那么应该没有问题。
  • mmmm 让我检查一下我是否能够访问所有变量,尽管只有保存不起作用
  • 看起来你的把戏奏效了 :) 我在方法本身中传递了 id 并从 db 获取。它得到了更新..thankssss
猜你喜欢
  • 2020-05-21
  • 2021-06-24
  • 1970-01-01
  • 2019-12-23
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
相关资源
最近更新 更多