Ruby on rails 3 RubyonRails3-检索的数据始终为零

Ruby on rails 3 RubyonRails3-检索的数据始终为零,ruby-on-rails-3,search,activerecord,Ruby On Rails 3,Search,Activerecord,我找不到以下代码的有效解决方案: def search_last_rate self.rate = Rate.find(:first, :select => "rateconverted", :conditions => ["dominant_id = ? and converted_id = ?", self.currency_bought_iso, self.currency_sold_iso], :order => 'ratedate, dateloaded

我找不到以下代码的有效解决方案:

  def search_last_rate
    self.rate  = Rate.find(:first, :select => "rateconverted", :conditions => ["dominant_id = ? and converted_id = ?", self.currency_bought_iso, self.currency_sold_iso], :order => 'ratedate, dateloaded, timeloaded DESC')
  end
当我在控制台中执行此操作时,我检索适当的值:

`=> #<Rate rateconverted: 0.8131>`           
`=>#`
MYSQL2中的所有列都定义为
十进制
,但当我在模型和新html页面中使用它时,它总是保存值
0.0

请注意:)

  • 确保self.rate不是整数
  • 试一试

    self.rate=rate.find(:first,:select=>“ratecoverted”,:conditions=>[“主id=?和转换id=?”,self.currency\u bunded\u iso,self.currency\u sell\u iso],:order=>“ratedate,dateloaded,timeloaded DESC”)。发送至


  • 十进制和整数在mysql中是不同的。如果您希望数据为数字,则在迁移中使用“整数”,否则使用“浮点”。签出

    已经感谢您的帮助:)我尝试了该语句并收到以下错误:NoMethodError(未定义的方法“to#f”for#):我已经阅读并理解了上下文,但不知道如何在我现在的代码中使用它。同时阅读并认为我应该继续使用十进制。(添加列:parts,:price,:decimal,:precision=>8,:scale=>2)。刻度是小数点右侧的最大数字和小数点左侧的精度。我必须在哪里添加此值?在迁移文件中,我已经这样做了,例如rate列<代码>十进制:速率,:精度=>12,:刻度=>8。所有受影响的列都是这种情况。非常感谢您的帮助,因为我被卡住了:(费率是否正确显示?在数据库中是否正确显示?当它存储在值
    self.rate中时,请通过控制台将其发送到.)