Dolf

Joined

60 Experience
0 Lessons Completed
0 Questions Solved

Activity

Thank you so much inopinatus for taking the time, this is super helpful!

Interesting approach to use different "reason" models without inheritance but via
as :reasons
Makes a lot of sense, wouldn't have come up with that.

Also, will read up on Struct.new :)

Thanks again!
Dear GoRails-Community,

Super happy to finally have joined! Thanks Chris for building the site and community. Really love that it allows to discuss approaches which always gets downvoted on SO :)

I'm building a small SEO tool as a side project (live in alpha for free at the moment, bearing some API cost). Instead of charging monthly, I plan to give users the option to purchase credits to then be used for multiple actions on the site.

Here is how I sketched it out on "paper" with inheritance after googling a lot:

class Transaction < ApplicationRecord
  belongs_to :user
  # sub_types: buy_credits, spend_credits, add_free_credits
  # columns: credit_change
end

class Add_free_credits < Transaction
  has_one :admin_user  # if added manually by the admin as free credit; nil if added by the system for signup
  # columns: credit_change (inherited from transaction, values positive), type ("sign_up_bonus", "goodwill")
end

class Purchase_credits < Transaction
  has_one :payment  # to be added later
  # columns: credit_change (inherited from transaction, values positive), currency, price, amount
end

class Spend_credits < Transaction
  belongs_to :site_action  # the action the user is charged for (dummy name)
  # columns: credit_change (inherited from transaction, values negative)
end


class User
  ...
  def credits_balance # or self.credits_balance ?? :)
    @user.transactions.sum(:amount)
  end
  # Thanks to Casey Provost, https://gorails.com/forum/how-do-i-create-a-virtual-balance-model-in-rails
end

So, I have 2 questions I have with this:

1) Should I really go the "inherited" route? Transactions feel similar enough, but at the same time also different enough to justify it. The alternative (transaction model only, with a column "type") feels messy

2) The inherited model names sound more like actions "add_free_credits", ... This worries me a bit. Should I either change the names to, e.g., "Purchase_transaction" (or credit/debit) and then add the actions, or these are rather functions inside one model?

3) Quick naming question, would you rather use "Purchases" of users or site "Sales"?

Any feedback is highly appreciated, thanks so much :)