Reputation: 61
I am working on a rails app for a charity who takes crowd-funded donations for Families, Fundraisers, and for the charity itself. Right now donations are setup to belong to Families & Fundraisers using Polymorphism, but I'm not sure how to take money for the charity.
Should I make a model for donations to the charity? That feels like bad architecture to me because it will be a single database record.
What is the best way to take donations for the charity?
Here is my donation model:
#app/models/donation.rb
class Donation < ActiveRecord::Base
has_secure_token
belongs_to :family, via: :recipient
belongs_to :fundraiser, via: :recipient
Thanks!
Upvotes: 0
Views: 72
Reputation: 10111
in your migration you should do something like
class Donations < ActiveRecord::Migration
def change
create_table :donations do |t|
...
t.integer :something_id # this is the record id of something
t.string :something_type # this is the model name like family or fundraiser
t.timestamps
end
end
end
Now in your model
class Donation < ActiveRecord::Base
belongs_to :something, polymorphic: true
end
# repeat this in all the models that can use this polymorphic association
class Family < ActiveRecord::Base
has_many :donations, as: :something
end
I hope that this has helped.
Happy Hacking
Upvotes: 1