我添加了一个" 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
控制器
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的新手,并试图了解我的代码发生了什么。我提前感谢任何帮助。
谢谢!
答案 0 :(得分:3)
这里有两件事让你有些悲伤,但他们很容易解决。
首先,在控制器的创建操作中,您实际上调用了两种将数据保存到数据库的方法,这就是为什么您在控制台输出中看到两个保存的原因。
方法的第一行负责第一次保存:
@transaction = @account.transactions.create(transaction_params)
这一行在你的respond_to块中负责第二次保存:
if @transaction.save
其次,记录在第一次保存中而不在第二次保存中具有正确amount
的原因与您的事务模型的before_save
回调中的逻辑有关。它正在使用amount
并在其上调用* -1
。由于第一次保存已经使金额为负数,因此第二次保存会将其重新设置为正数。
答案 1 :(得分:1)
您似乎需要before_create
因为您不太可能改变交易类型吗?
before_create do
if self.trx_type == "debit"
self.amount = self.amount * -1
end
end
更新:看起来您需要在控制器中更改:
@transaction = @account.transactions.create(transaction_params)
到
@transaction = @account.transactions.build(transaction_params)