如何更新关联记录

时间:2019-10-11 13:12:40

标签: ruby-on-rails ruby

import java.awt.Dimension;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class ScreenResolutionDemo extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth() / 3;
        double height = screenSize.getHeight() / 2;

        try {
            **primaryStage.setScene**(new Scene(new AnchorPane(), *width*, **height**));
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        launch(args);
    }

}

我正在尝试通过执行以下操作来更新附在帐单上的所有帐单: class Bill < ApplicationRecord has_many :bill_items has_many :items, through :bill_items def add(item) BillItem.create(bill: self, item: item) update_total end def update_total total = 0 bill_items.each do |item| total += item.price end self.update(total: total) end end class BillItem < ApplicationRecord belongs_to :bill belongs_to :item end class Item < ApplicationRecord has_many :bill_items end

这似乎更新了实际记录,但没有更新相关记录。即。如果我运行bill.bill_items.update_all(price: new_price),我将获得新价格,但如果我运行BillItem.pluck(:price),我将获得旧价格。如何更新关联记录?

1 个答案:

答案 0 :(得分:0)

您可以使用reload方法。它将从数据库中重新加载记录。

bill.bill_items.update_all(price: new_price)
bill.reload