On Friday, 11 July 2014 13:36:32 UTC-5, Ruby-Forum.com User wrote:
>
> 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'
>
>
I don't think this is an inheritance problem. What does the schema for your
transaction_items table look like? The error message suggests that it
*doesn't* have a column `expense_type_id`, which is what `belongs_to
:expense_type` is going to be looking for.
A wild guess: maybe you've got a `transaction_type_id` column instead? In
that case, you should use this on `Expense`:
belongs_to :expense_type, foreign_key: :transaction_type_id
(and similar for `:income_type` on `Income`)
--Matt Jones
--
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/f9821709-7c5d-43b7-896f-8a81690781a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.