alik
alik

Reputation: 3870

Access a model method from a controller

I am relatively new to rails and am trying to figure this out the right syntax for the following

I have a model Transactions with the method

 def self.add_external_transaction_for information
...
end

declared in transaction.rb

What I am trying to do is call add_external_transaction_for from a different controller named DepositsController for the Deposit model like this in deposits_controller.rb

ActiveRecord::Transactions.add_external_transaction_for(@deposit)

however i see the following error

undefined method `add_external_transaction_for' for ActiveRecord::Transactions:Module

Can someone please help me. I need to be able to have a method that is associated with the Transaction model that is accessed from a controller without me having to create a new instance of Transaction just to access that method

Upvotes: 1

Views: 3063

Answers (1)

Tim Snowhite
Tim Snowhite

Reputation: 3776

If your transaction model starts like this:

class Transaction < ActiveRecord::Base

Then Transaction.add_external_transaction_for(@deposit) should work fine, it shouldn't need to be scoped to the ActiveRecord module. Also, you might be seeing a naming conflict with the ActiveRecord::Transactions module.

Upvotes: 4

Related Questions