I have created few models which are shown below.
Base models are TransactionType and TransactionItem
ExpenseType and IncomeType derives from TransactionType.
Expense and Income derives from TransactionItem.

class TransactionType < ActiveRecord::Base
  scope :expense_types,  -> { where(tran_type: 'ExpenseType') }
  scope :income_types,   -> { where(tran_type: 'IncomeType') }
  self.inheritance_column = "tran_type"
  validates :name, uniqueness: true
end

class ExpenseType < TransactionType
end

class IncomeType < TransactionType
end

class TransactionItem < ActiveRecord::Base
validates :note, length: { in: 2..255 }
end

class Expense < TransactionItem
belongs_to :expense_type
validates :expense_type, presence: true
end

class Income < TransactionItem
belongs_to :income_type
validates :income_type, presence: true
end


I can create objects for ExpenseType.
But, it throws error when Expense or Income object created.

<code>
ExpenseType.new(:name => "Grocceries").save!
ExpenseType.new(:name => "Travel").save!
IncomeType.new(:name => "Salary").save!
IncomeType.new(:name => "Bonus").save!
Expense.new(:note => "a soda", :expense_type => ExpenseType.first)

2.1.2 :006 > Expense.new(:note => "a soda", :expense_type =>
ExpenseType.first)
  ExpenseType Load (0.1ms)  SELECT  "transaction_types".* FROM
"transaction_types"  WHERE "transaction_types"."tran_type" IN
('ExpenseType')  ORDER BY "transaction_types"."id" ASC LIMIT 1
ActiveModel::MissingAttributeError: can't write unknown attribute
`expense_type_id'
from

It would be great if someone shares your idea for solving this.

Thanks,
Masc

-- 
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/2b38adfa905ee482966f32cfd6f44245%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to