Hello there, I have two objects as follows: BankAccount =========int id float amount Transaction transactions[] Transaction ========int id BankAccount account_credit_to # in the db table account_credit_to is int BankAccount account_debit_from # int in db table float amount date_time Each transaction is associated with exactly 2 accounts. How do I map these as Ruby models and access the objects. Here is my attempt: def BankAccount has_many :transactions end def Transaction #*** here how do I tell that this transaction references two accounts *** ? end And how do I access them in the controller? ba = BankAccount.new ba.transactions??? Thanks in Advance! MS --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
FWIW, we''ve added another layer (actually several but one it boils down to one for the area you''re dealing with). It looks something like this: Account has_many :account_details -opening_balance -reconciled_balance -reconciled_on - current_balance is a method that sums credits, debits and uses them to offset the reconciled balance AccountDetail belongs_to Account belongs_to Transaction - detail_type (=Debit or Credit) - amount Transaction has_many :account_details Beyond the modeling, the main thing you need to do (surprise!) is make sure the debits and credits associated with the Transaction balance. On May 5, 2:17 pm, marahddis <siddha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello there, > > I have two objects as follows: > > BankAccount > =========> int id > float amount > Transaction transactions[] > > Transaction > ========> int id > BankAccount account_credit_to # in the db table account_credit_to > is int > BankAccount account_debit_from # int in db table > float amount > date_time > > Each transaction is associated with exactly 2 accounts. > > How do I map these as Ruby models and access the objects. > > Here is my attempt: > > def BankAccount > has_many :transactions > end > > def Transaction > #*** here how do I tell that this transaction references two > accounts *** ? > end > > And how do I access them in the controller? > > ba = BankAccount.new > ba.transactions??? > > Thanks in Advance! > > MS--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-05 20:30 UTC
Re: belongs_to :2accounts ... :3accounts ... :xaccounts ??
On 5 May 2008, at 21:06, AndyV wrote:> > FWIW, we''ve added another layer (actually several but one it boils > down to one for the area you''re dealing with). It looks something > like this: > > Account > has_many :account_details > -opening_balance > -reconciled_balance > -reconciled_on > - current_balance is a method that sums credits, debits and uses them > to offset the reconciled balance > > AccountDetail > belongs_to Account > belongs_to Transaction > - detail_type (=Debit or Credit) > - amount > > Transaction > has_many :account_details > > Beyond the modeling, the main thing you need to do (surprise!) is make > sure the debits and credits associated with the Transaction balance. >And also don''t use the word transaction. You''re asking for trouble. Fred.> > On May 5, 2:17 pm, marahddis <siddha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hello there, >> >> I have two objects as follows: >> >> BankAccount >> =========>> int id >> float amount >> Transaction transactions[] >> >> Transaction >> ========>> int id >> BankAccount account_credit_to # in the db table account_credit_to >> is int >> BankAccount account_debit_from # int in db table >> float amount >> date_time >> >> Each transaction is associated with exactly 2 accounts. >> >> How do I map these as Ruby models and access the objects. >> >> Here is my attempt: >> >> def BankAccount >> has_many :transactions >> end >> >> def Transaction >> #*** here how do I tell that this transaction references two >> accounts *** ? >> end >> >> And how do I access them in the controller? >> >> ba = BankAccount.new >> ba.transactions??? >> >> Thanks in Advance! >> >> MS > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Trevor Squires
2008-May-05 20:47 UTC
Re: belongs_to :2accounts ... :3accounts ... :xaccounts ??
Hey, to answer your specific question: class Transaction < ActiveRecord::Base belongs_to : account_credit_to, :class_name => ''BankAccount'' belongs_to :account_debit_from, :class_name => ''BankAccount'' end # Note - transactions table must have ''account_credit_to_id'' and ''account_debit_from'' integer columns. class BankAccount has_many :credit_transactions, :as => :account_credit_to, :class_name => ''Transaction'' has_many :debit_transactions, :as => :account_debit_from:, class_name => ''Transaction'' end HOWEVER... I would strongly advise against this code: First of all, you might as well treat ''transaction'' and ''Transaction'' as reserved words in Rails. AccountTransaction or something similar will help to avoid conflicts. Next, I don''t know the details of the system you''re trying to create but your table definition seems oversimplified for tracking the movement of money. I think you probably should talk to the accountant that''s going to eventually have to deal with these records you''re creating. Find out the way they want you to record this information in a database (or how they''d do it with a paper journal). Regards, Trevor On 5/5/08, marahddis <siddharam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello there, > > I have two objects as follows: > > BankAccount > =========> int id > float amount > Transaction transactions[] > > > Transaction > ========> int id > BankAccount account_credit_to # in the db table account_credit_to > is int > BankAccount account_debit_from # int in db table > float amount > date_time > > > Each transaction is associated with exactly 2 accounts. > > How do I map these as Ruby models and access the objects. > > Here is my attempt: > > def BankAccount > has_many :transactions > end > > def Transaction > #*** here how do I tell that this transaction references two > accounts *** ? > end > > And how do I access them in the controller? > > ba = BankAccount.new > ba.transactions??? > > > Thanks in Advance! > > MS > > >-- -- Trevor Squires http://somethinglearned.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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-05 20:53 UTC
Re: belongs_to :2accounts ... :3accounts ... :xaccounts ??
On 5 May 2008, at 21:47, Trevor Squires wrote:> > Hey, > > to answer your specific question: > > class Transaction < ActiveRecord::Base > belongs_to : account_credit_to, :class_name => ''BankAccount'' > belongs_to :account_debit_from, :class_name => ''BankAccount'' > end > > # Note - transactions table must have ''account_credit_to_id'' and > ''account_debit_from'' integer columns. > > class BankAccount > has_many :credit_transactions, :as => :account_credit_to, > :class_name => ''Transaction'' > has_many :debit_transactions, :as => :account_debit_from:, > class_name => ''Transaction'' > endShouldn''t that be :foreign_key => ''account_debit_from_id'' (:as creates a polymorphic association which i don''t think is required here) Fred>--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Trevor Squires
2008-May-05 21:34 UTC
Re: belongs_to :2accounts ... :3accounts ... :xaccounts ??
Fred, yarr - good catch. Fingers clearly moving faster than brain. Trev On 5/5/08, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Shouldn''t that be :foreign_key => ''account_debit_from_id'' (:as creates > a polymorphic association which i don''t think is required here) > > Fred >-- -- Trevor Squires http://somethinglearned.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?hl=en -~----------~----~----~----~------~----~------~--~---
Right, we actually use FinancialTransaction. On May 5, 4:30 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5 May 2008, at 21:06, AndyV wrote: > > > > > > > FWIW, we''ve added another layer (actually several but one it boils > > down to one for the area you''re dealing with). It looks something > > like this: > > > Account > > has_many :account_details > > -opening_balance > > -reconciled_balance > > -reconciled_on > > - current_balance is a method that sums credits, debits and uses them > > to offset the reconciled balance > > > AccountDetail > > belongs_to Account > > belongs_to Transaction > > - detail_type (=Debit or Credit) > > - amount > > > Transaction > > has_many :account_details > > > Beyond the modeling, the main thing you need to do (surprise!) is make > > sure the debits and credits associated with the Transaction balance. > > And also don''t use the word transaction. You''re asking for trouble. > > Fred. > > > > > On May 5, 2:17 pm, marahddis <siddha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hello there, > > >> I have two objects as follows: > > >> BankAccount > >> =========> >> int id > >> float amount > >> Transaction transactions[] > > >> Transaction > >> ========> >> int id > >> BankAccount account_credit_to # in the db table account_credit_to > >> is int > >> BankAccount account_debit_from # int in db table > >> float amount > >> date_time > > >> Each transaction is associated with exactly 2 accounts. > > >> How do I map these as Ruby models and access the objects. > > >> Here is my attempt: > > >> def BankAccount > >> has_many :transactions > >> end > > >> def Transaction > >> #*** here how do I tell that this transaction references two > >> accounts *** ? > >> end > > >> And how do I access them in the controller? > > >> ba = BankAccount.new > >> ba.transactions??? > > >> Thanks in Advance! > > >> MS--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---