On 16 December 2015 at 20:05, Travis Eubanks <[email protected]> wrote: > Im creating an expense report. > > I don't know if Im on the right path but heres what im thinking... > When submitting an expense report the user could have travel expenses, > parking expenses, cell phone expenses or even meal expenses. > > Each expense available shares 3 common attributes > -cost, occurrence, GL code > > (some expenses will need additional attributes). > > My first thought was to use STI: > > class User < ActiveRecord::Base > end > > class Expense < ActiveRecord::Base > (which have the cost, occurrence, GL code attributes) > end > > class Travel < Expense > end > > class Entertainment < Expense > (and this class and some others will be needing additional attributes) > end > > ///////////////// > > 1. A user selects a month and year this expense will belong to > 2. A user then selects which 'type' of expense they're going to fill out > (i.e. Travel expense) > 3. User submits that form > 4 Repeat as needed > > I want to be able to: > view all expenses for a user > view all user expenses associated with a month/year > View all user expenses via a 'type' of expense > > I wanted to use STI but not all models will be all the same as some will > have additional attributes so then I thought okay maybe polymorphism > will be needed. Any help would be gratifying thank you!
I would probably start by doing it the simple way with a single table for expenses where the table contains all the fields for all expense types. Then have an expense_type field (don't call it just type as that is a reserved word in Rails). Don't worry about the fact that some fields will be empty on some expense types. Obviously User has_many expenses, Expense belongs_to user. It might be worth using STI but I would start the simple way and see how it pans out. Refactoring for that sort of change should not be difficult. You might want to consider having expense_type as a table so that you can add expense types without changing the code. Expense belongs_to expense_type, but that may or may not be appropriate for your requirement. Colin > > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/49a7d926ea0564896ab20d1ff65f79c2%40ruby-forum.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtDBFSFYiknhNR4225xQwfVzmYmS93tf0OmO9NvvOSizw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

