Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 将模型上的实验室价格保存到数据库rails_Postgresql_Ruby On Rails 4 - Fatal编程技术网

Postgresql 将模型上的实验室价格保存到数据库rails

Postgresql 将模型上的实验室价格保存到数据库rails,postgresql,ruby-on-rails-4,Postgresql,Ruby On Rails 4,通过关系,我有3个模型:实验室、医疗记录和实验室医疗。在medic_记录模型中,我有一个计算实验室价格的方法,我还为medic_记录添加列“lab_价格:十进制”创建了一个迁移 medic_record.rb has_many :lab_medics, :dependent => :destroy has_many :labs, :through => :lab_medics before_save :finalize def lab_price Lab.where(:id

通过关系,我有3个模型:实验室、医疗记录和实验室医疗。在medic_记录模型中,我有一个计算实验室价格的方法,我还为medic_记录添加列“lab_价格:十进制”创建了一个迁移

medic_record.rb

has_many :lab_medics, :dependent => :destroy
has_many :labs, :through => :lab_medics

before_save :finalize

def lab_price
  Lab.where(:id => self.lab_ids).reduce(0) { |sum, x| sum += x[:price].to_f }
end

private
   def finalize
     self[:lab_price] = lab_price
   end
lab.rb

has_many :lab_medics, :dependent => :destroy
has_many :medic_records, :through => :lab_medics
医学实验室

belongs_to :lab
belongs_to :medic_record

我的问题是如何将medic_记录模型中的实验室价格保存到数据库“column:lab_price”中?我尝试过使用“before_save:finalize”方法,但在为medic_record创建新记录后,数据库上的结果始终为“0.0”。

已解决

我已经改变了这个方法,它成功了

def self.save_tot(medic)
    medic.update_attributes(lab_price: Lab.where(:id => medic.lab_ids).reduce(0) { |sum, x| sum += x[:price].to_f })
end