views:

103

answers:

0

I'm re-factoring a semi-successful Rails-based event registration and pledging system for a particular fundraising event for a non-profit.

The existing data structure is rather ungainly, though it captures all necessary details and relationships. I would plan to refactor this in any case, but since I've also been asked to allow for multiple registrations (for multiple people/users) on a single payment/checkout pass, data structure changes seem necessary.

The current relevant data models are as follows:

class User < ActiveRecord::Base
  has_one :registration
  has_one :profile
  has_many :pledges
  has_one :payment
end

class Registration < ActiveRecord::Base
  belongs_to :user
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

class Pledge < ActiveRecord::Base
  belongs_to :user
  has_one :payment
end

class Payment < ActiveRecord::Base
  belongs_to :user
  belongs_to :pledge
  has_many :transactions
end

class Transaction < ActiveRecord::Base
  belongs_to :payment
end

Currently, the registration payment amount is calculated in the Registration model based on its attributes. I'm thinking it would be preferable to instead associate the Registration object to line_items to replace the Registration attributes that affect the payment calculation, thusly:

class Registration < ActiveRecord::Base
  belongs_to :user
  has_many :line_items
end

class LineItem < ActiveRecord::Base
  belongs_to :registration
  belongs_to :item
end

class Item < ActiveRecord::Base
  has_many :line_items
end

Is there a better way to accomplish this? I'm not sure this is altogether necessary, since the user doesn't need to be able to CRUD their line_items individually. They will simply be associated in the controller or model after the user selects their registration options.

My primary conundrum is related to the multiple-users/registrations per checkout/payment thing. My initial thought is to add an Order model that has_many :registrations. Like so (unchanged classes omitted):

class User < ActiveRecord::Base
  has_one :registration
  has_one :profile
  has_many :pledges
  belongs_to :order
end

class Order < ActiveRecord::Base
  has_many :registrations
  has_many :users
  has_one :payment
end

class Registration < ActiveRecord::Base
  belongs_to :user
  belongs_to :order
  has_many :line_items
end

class LineItem < ActiveRecord::Base
  belongs_to :registration
  belongs_to :item
end

class Item < ActiveRecord::Base
  has_many :line_items
end

class Payment < ActiveRecord::Base
  belongs_to :order
  belongs_to :pledge
  has_many :transactions
end

Or would it be preferable to do:

class Order < ActiveRecord::Base
  has_many :registrations
  has_many :users, :through => registrations
  has_one :payment
end

What concerns me here is how a user will create new User and Registration objects for the people they're registering for. I want to make sure the data model will facilitate that process.

Any suggestions? Feel free to butcher my code...I've got plenty to learn.