In a toy Rails application, I'm modelling a situation where you have a number of pots, each containing an amount of something, and you can make transactions between the pots. A transaction can come from any pot, and go to any pot.
Here are the relevant parts of the models:
class Pot < ActiveRecord::Base
has_many :to_transactions, :foreign_key => "to_id", :class_name => "Transaction"
has_many :from_transactions, :foreign_key => "from_id", :class_name => "Transaction"
end
class Transaction < ActiveRecord::Base
belongs_to :to_pot, :class_name => "Pot", :foreign_key => "to_id"
belongs_to :from_pot, :class_name => "Pot", :foreign_key => "from_id"
end
This allows me to do the following at the console:
>> p = Pot.find(123)
>> p.from_transactions
=> # returns array of transactions from pot 123
>> t = p.to_transactions.new
=> # t is a new transaction with to_id set to 123
and so on.
I'm having a problem setting up the routing. For example, I would like:
/pots/123/from_transactions
to give a list of all transactions from pot 123,/pots/123/to_transactions/new
to give the new transaction form, with theto_id
set to 123
Is this possible? Any help gratefully received etc etc.