Design Question - Position created from one or more Trades

I am creating a system to track equity positions … I will enter a
Trade and a Position will be created as a result … I may have one or
more Trades that make up a Position. Positions can be open or closed.

What is the best way to generate the Position object?

Should I insert the code to create it manually (below) when the Trade
object is created?

def create
@trade = Trade.new(params[:trade])
if @trade.save
flash[:notice] = ‘Trade was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

Well, since you said that one or more trades may make up a position,
then you don’t necessarily always want to create a position - sometimes
you want to update a position when a trade is created, if you can create
trades separately. If you are entering all trades in at once and
creating the position from them, then below might be what you’re
after…

In any event, I think what you will do is first create the position,
then you will add trades to the position.

@position = Position.new(parms)
params[trades].each do |t|
@position.trades << Trade.new(t)
end

Something like that. Your associations:

position has_many :trades
trade belongs_to :position

And then in your Position model you’ll have methods that will
consolidate information from your trades.

Just my $.02.

c.

Mark Y. wrote:

I am creating a system to track equity positions … I will enter a
Trade and a Position will be created as a result … I may have one or
more Trades that make up a Position. Positions can be open or closed.

What is the best way to generate the Position object?

Should I insert the code to create it manually (below) when the Trade
object is created?

def create
@trade = Trade.new(params[:trade])
if @trade.save
flash[:notice] = ‘Trade was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

On 10/31/06, Mark Y. [email protected] wrote:

I am creating a system to track equity positions … I will enter a
Trade and a Position will be created as a result … I may have one or
more Trades that make up a Position. Positions can be open or closed.

What is the best way to generate the Position object?

When a new trade is created, you need to check whether this trade is
for an existing position, or whether a new position should be created.

Should I insert the code to create it manually (below) when the Trade
object is created?

I would move that code into the Trade model class. The following
assumes that there is no data required to create a position:

class Trade < ActiveRecord::Base

def before_create
if !self.position
self.position = Position.create
end
end

end

In your view, make sure you have a trade_position_id field that is
populated if the trade should be added to an existing position.

The before_save hook gets called after the validation, so you can be
reasonably sure that the subsequent save() call will succeed.
Regardless, you should wrap the @trade.save() call in your controller
in a transaction so that the creation of the position gets rolled back
if saving of the trade fails for some reason.

Cheers,
Max

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs