通过不同的对象更新属性

时间:2013-02-10 02:18:49

标签: ruby-on-rails

我正在努力弄清楚如何做到这一点。我整天都在这里。

我有一个Account类和一个Transaction类。 使用余额创建帐户,我希望交易金额(取决于其类型)可以从余额中添加或减去。

我希望每次创建交易时都能更新帐户余额。这是个人理财申请。截至目前,当我创建新交易时,帐户余额没有任何变化。

accounts_controller.rb

class AccountsController < ApplicationController
  def index
    @accounts = Account.all
  end

  def show
    @account = Account.find(params[:id])
  end

  def new
    @account = Account.new
  end

  def edit
    @account = Account.find(params[:id])
  end

  def create
    @account = Account.new(params[:account])

    respond_to do |format|
      if @account.save
        format.html { redirect_to @account, notice: 'Account was successfully created.' }
        format.json { render json: @account, status: :created, location: @account }
      else
        format.html { render action: "new" }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @account = Account.find(params[:id])

    respond_to do |format|
      if @account.update_attributes(params[:account])
        format.html { redirect_to @account, notice: 'Account was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /accounts/1
  # DELETE /accounts/1.json
  def destroy
  @account = Account.find(params[:id])
  @account.destroy

  respond_to do |format|
    format.html { redirect_to accounts_url }
    format.json { head :no_content }
  end
  end

  def update_balance
  @a = Account.find(params[:id])
  @a.transactions.each do |t| 
    @update_balance = t.t_type + @a.balance
    @a.update_attributes(:balance => @update_balance)
  end
  end
end

transactions_controller.rb

class TransactionsController < ApplicationController
  def create
    @account = Account.find(params[:account_id])
    @transaction = @account.transactions.create(params[:transaction])
    redirect_to account_path(@account)
  end
end

transaction.rb

class Transaction < ActiveRecord::Base
  belongs_to :account
  attr_accessible :amount, :category, :t_type
end

account.rb

class Account < ActiveRecord::Base
  attr_accessible :balance, :name
  has_many :transactions
end

如果有人知道我做错了什么或者能指出我的方向进行了彻底的解释,那就太好了。我在这一点上迷失了。

2 个答案:

答案 0 :(得分:0)

试试这个。

class Account < ActiveRecord::Base
  attr_accessible :balance, :name
  has_many :transactions

  def update_with_transaction(transaction)
    return unless self.transactions.include? transaction
    if transaction.t_type.eql? SOME_TYPE
      self.balance += transaction.ammount
    else 
      self.balance -= transaction.ammount
    end
    save
  end
end

class TransactionsController < ApplicationController
  def create
    account = Account.find(params[:account_id])
    @transaction = account.transactions.create(params[:transaction])
    account.update_with_transaction(@transaction)
    redirect_to account_path(account)
  end
end

答案 1 :(得分:0)

它没有更新,因为你没有告诉它。创建一个after_create callback来执行此操作:

class Transaction < ActiveRecord::Base
  # ...
  after_create :update_account_balance

  private

  def update_account_balance
    account.balance += amount
    account.save
  end
end

根据需要进行调整。请注意,这不会处理交易金额的更新,这是读者的练习。