Actually I'm testting the plutus gem for double entry accounting.I've accounts and transactions.When You want to record an transaction there's to steps:

1-create the 2 accounts that will receive the transaction amount(eg. Cash(debit) & Withdraw(credit).

2-You create a transaction and pass his amount to the debit_account and credit_account, you've created in step number 1.You do it by finding the type of account, and then it's name(see transactions_controller.rb)

models/account.rb

class Account < ActiveRecord::Base

    set_inheritance_column do
        "type" + "_id"
    end

    validates_presence_of :name, :type
    validates_presence_of :name

    has_many :credit_transactions,  :class_name => "Transaction", :foreign_key => "credit_account_id"
    has_many :debit_transactions,  :class_name => "Transaction", :foreign_key => "debit_account_id"
      has_many :transactions
      accepts_nested_attributes_for :transactions 
end

models/transaction.rb

class Transaction < ActiveRecord::Base
  belongs_to :commercial_document, :polymorphic => true
  belongs_to :credit_account, :class_name => "Account"
  belongs_to :debit_account, :class_name => "Account"
  belongs_to :account


  validates_presence_of :credit_account, :debit_account, :amount, :description
  validates_associated :credit_account, :debit_account
end

controller/transactions_controller.rb

  def new 

      @transaction = Transaction.new

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

        def create
        @transaction = Transaction.new(params[:transaction])
        @transaction.credit_account = Account.where(:type => params[:type]).where(:name => params[:name]) 
        @transaction.debit_account =  Account.where(:type => params[:type]).where(:name => params[:name])

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

transaction/_form.html.erb

<%= simple_form_for @transaction do |f| %>

  <%= f.input :description %>
  <%= f.input :debit_account %>
  <%= f.input :credit_account %>
  <%= f.input :amount %>

  <%= f.button :submit %>   

<% end %>

when I submit a transaction I get the following error message

ActiveRecord::AssociationTypeMismatch in TransactionsController#create
Account(#2160906760) expected, got String(#2151988680)

What am I doing wrong?

有帮助吗?

解决方案

Please replace the following code:

@transaction.credit_account = Account.where(:type => params[:type]).where(:name => params[:name]) 
@transaction.debit_account =  Account.where(:type => params[:type]).where(:name => params[:name])

with

@transaction.credit_account = Account.find(:all, :conditions => [:type => params[:type], :name => params[:name]])  
@transaction.debit_account =  Account.find(:all, :conditions => [:type => params[:type], :name => params[:name]])

I hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top