Monday, 15 August 2011

ruby on rails - Updating attributes through a different object -



ruby on rails - Updating attributes through a different object -

i having ridiculously hard time trying figure out how this. have been @ literally day.

i have business relationship class , transaction class. accounts created balance , want transaction amount, depending on type, either add together or subtract balance.

i want able update business relationship balance every time transaction created. personal finance application. of when create new transaction, nil happens business relationship balance.

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 |format| if @account.save format.html { redirect_to @account, notice: 'account 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 |format| if @account.update_attributes(params[:account]) format.html { redirect_to @account, notice: 'account 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 |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 |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 business relationship < activerecord::base attr_accessible :balance, :name has_many :transactions end

if has thought i'm doing wrong or can point me in direction of thorough explanation, great. lost @ point.

try this.

class business relationship < activerecord::base attr_accessible :balance, :name has_many :transactions def update_with_transaction(transaction) homecoming 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 business relationship = account.find(params[:account_id]) @transaction = account.transactions.create(params[:transaction]) account.update_with_transaction(@transaction) redirect_to account_path(account) end end

ruby-on-rails

No comments:

Post a Comment