gpt4 book ai didi

ruby-on-rails - Ruby on Rails before_save 在创建新记录后直接导致更新

转载 作者:行者123 更新时间:2023-12-01 12:14:20 24 4
gpt4 key购买 nike

我在我的模型中添加了一个“before_save”,以便在保存之前对我的模型应用一些逻辑。当我使用此代码时,会创建记录,然后立即更新(使用不正确的值)。如果我把它注释掉,我创建新记录时就没有后续更新了。

型号

class Transaction < ApplicationRecord
belongs_to :account

attr_accessor :trx_type

before_save do
if self.trx_type == "debit"
self.amount = self.amount * -1
end
end

end

Controller

class TransactionsController < ApplicationController
before_action :find_account
before_action :find_transaction, only: [:edit, :update, :show, :destroy]

# Index action to render all transactions
def index
@transactions = @account.transactions

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @transactions }
end
end

# New action for creating transaction
def new
@transaction = @account.transactions.build

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @transaction }
end
end

# Create action saves the trasaction into database
def create
@transaction = @account.transactions.create(transaction_params)

respond_to do |format|
if @transaction.save
format.html { redirect_to([@transaction.account, @transaction], :notice => 'Transaction was successfully created.') }
format.xml { render :xml => @transaction, :status => :created, :location => [@transaction.account, @transaction] }
else
format.html { render :action => "new" }
format.xml { render :xml => @transaction.errors, :status => :unprocessable_entity }
end
end
end

# Edit action retrieves the transaction and renders the edit page
def edit
end

# Update action updates the transaction with the new information
def update
respond_to do |format|
if @transaction.update_attributes(transaction_params)
format.html { redirect_to([@transaction.account, @transaction], :notice => 'Transaction was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @transaction.errors, :status => :unprocessable_entity }
end
end
end

# The show action renders the individual transaction after retrieving the the id
def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @transaction }
end
end

# The destroy action removes the transaction permanently from the database
def destroy
@transaction.destroy

respond_to do |format|
format.html { redirect_to(account_transactions_url) }
format.xml { head :ok }
end
end

private

def transaction_params
params.require(:transaction).permit(:trx_date, :description, :amount, :trx_type)
end

def find_account
@account = current_user.accounts.find(params[:account_id])
end

def find_transaction
@transaction = @account.transactions.find(params[:id])
end
end

控制台输出

Started POST "/accounts/1/transactions" for 127.0.0.1 at 2018-03-20 13:59:37 -0400
Processing by TransactionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kURRN8FaHmjrDU7y5cikBLREGZdMgHm4PsVUcOHxn7MAlqmi2zolA0LYOKQ46JkTzXl+Fkgj1O6SlBhVjdM5Qw==", "transaction"=>{"trx_type"=>"debit", "trx_date(1i)"=>"2018", "trx_date(2i)"=>"3", "trx_date(3i)"=>"20", "description"=>"Test 10", "amount"=>"132"}, "commit"=>"Create Transaction", "account_id"=>"1"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Account Load (0.5ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."user_id" = $1 AND "accounts"."id" = $2 LIMIT $3 [["user_id", 1], ["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
SQL (0.6ms) INSERT INTO "transactions" ("trx_date", "description", "amount", "account_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["trx_date", "2018-03-20"], ["description", "Test 10"], ["amount", "-132.0"], ["account_id", 1], ["created_at", "2018-03-20 13:59:37.349781"], ["updated_at", "2018-03-20 13:59:37.349781"]]
(3.5ms) COMMIT
(0.1ms) BEGIN


SQL (0.3ms) UPDATE "transactions" SET "amount" = $1, "updated_at" = $2 WHERE "transactions"."id" = $3 [["amount", "132.0"], ["updated_at", "2018-03-20 13:59:37.355748"], ["id", 27]]
(0.9ms) COMMIT
Redirected to http://localhost:3000/accounts/1/transactions/27
Completed 302 Found in 16ms (ActiveRecord: 6.6ms)

我是 Rails 的新手,想了解我的代码发生了什么。感谢您提前提供的帮助。

谢谢!

最佳答案

这里有两件事让您感到有些悲伤,但它们很容易解决。

首先,在 Controller 的创建操作中,您实际上调用了两个将数据持久保存到数据库的方法,所以这就是您在控制台输出中看到两次保存的原因。

方法中第一行负责第一次保存:

@transaction = @account.transactions.create(transaction_params)

respond_to block 中的这一行负责第二次保存:

if @transaction.save

其次,记录在第一次保存中具有正确的数量而在第二次保存中不正确的原因与事务模型的before_save回调中的逻辑有关.它获取 amount 并对其调用 * -1。由于第一次保存已经使金额为负,第二次保存会将其恢复为正。

关于ruby-on-rails - Ruby on Rails before_save 在创建新记录后直接导致更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49391370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com