has many - Single table inheritance and has_many in rails -
i'm making simple financial app funzies. modeled credits , debits using single table inheritance, both inherit transactions. however, each transaction belongs user:
class transaction < activerecord::base belongs_to :user end class debit < transaction end class credit < transaction end
i create separate controllers credits , debits , this:
@debit = current_user.debits.build(params[:debit]) ... @credit = current_user.credits.build(params[:credit]) ...
but user not have methods debits or credits, transactions. alternatively, define single transactions controller:
@transaction = current_user.transactions.build(params[:transactions])
but type null, , how should set if it's protected mass assignment? it's bit of pickle either way. except pickles taste good.
you can explicitly set type of transaction in second example doing:
@transaction = current_user.transactions.build(params[:transactions]) @transaction.type = "debit" @transaction.save
the problem @transaction instance variable not of type debit until it's saved , reloaded different variable.
Comments
Post a Comment