Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Tricky Inheritence

Active Record Inheritence Single Table Inheritence inheritance_column

  • Please log in to reply
1 reply to this topic

#1 premmasc

premmasc

    Passenger

  • Members
  • 3 posts

Posted 08 July 2014 - 09:20 PM

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.
 
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 /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods/write.rb:93:in `write_attribute_with_type_cast'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods/write.rb:58:in `write_attribute'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods/dirty.rb:68:in `write_attribute'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:392:in `[]='
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/belongs_to_association.rb:70:in `replace_keys'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/singular_association.rb:17:in `writer'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/builder/association.rb:118:in `expense_type='
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:45:in `public_send'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:26:in `each'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/core.rb:455:in `init_attributes'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/core.rb:198:in `initialize'
from /Users/kishora/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/inheritance.rb:30:in `new'

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

 

Thanks,

Masc



#2 pramod

pramod

    Passenger

  • Members
  • 1 posts

Posted 21 July 2014 - 08:44 AM

Masc, I think you are trying to do STI, in that case you must have type column in associative table. Did you do that ?

 

 

Regards,

Pramod







Also tagged with one or more of these keywords: Active Record, Inheritence, Single Table Inheritence, inheritance_column

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users