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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Cayce Balara
2006-Oct-31 03:01 UTC
Re: Design Question - Position created from one or more Trad
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 Yea 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Max Muermann
2006-Nov-01 23:56 UTC
Re: Design Question - Position created from one or more Trades
On 10/31/06, Mark Yea <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---