# Design Question - Position created from one or more Trades

**URL:** <https://www.ruby-forum.com/t/design-question-position-created-from-one-or-more-trades/74140>\
**Category:** Rails\
**Created:** [October 30, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/design-question-position-created-from-one-or-more-trades/74140 "2006-10-30T23:51:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![85niner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/8/a4c791/32.png) [@85niner](https://www.ruby-forum.com/u/85niner)\
**Post date:** [October 30, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/design-question-position-created-from-one-or-more-trades/74140/1 "2006-10-30T23:51:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![85niner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/8/a4c791/32.png) [@85niner](https://www.ruby-forum.com/u/85niner)\
**Post date:** [October 31, 2006, 4:01am UTC](https://www.ruby-forum.com/t/design-question-position-created-from-one-or-more-trades/74140/2 "2006-10-31T04:01:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![85niner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/8/a4c791/32.png) [@85niner](https://www.ruby-forum.com/u/85niner)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/design-question-position-created-from-one-or-more-trades/74140/3 "2006-11-07T10:53:29Z")

</div>

On 10/31/06, Mark Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
