Tricky Rails 4 ActiveRecord Наследование
Я создал несколько моделей, которые показаны ниже. Базовыми моделями являются TransactionType и TransactionItem ExpenseType, а IncomeType является производным от TransactionType. Расходы и доходы получаются из 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
Я могу создавать объекты для ExpenseType. Но он выдает ошибку при создании объекта Expense или Income.
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/masc/.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/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods/write.rb:58:in `write_attribute'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods/dirty.rb:68:in `write_attribute'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:392:in `[]='
from /Users/masc/.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/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/singular_association.rb:17:in `writer'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/associations/builder/association.rb:118:in `expense_type='
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:45:in `public_send'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
from /Users/masc/.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/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:26:in `each'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/core.rb:455:in `init_attributes'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/core.rb:198:in `initialize'
from /Users/masc/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/inheritance.rb:30:in `new'
Было бы здорово, если бы кто-то поделился вашей идеей решить эту проблему.
Спасибо, Masc
1 ответ
Если Expense принадлежит ExpenseType, тогда таблице расходов необходим целочисленный атрибут cos t_type_id.
Вы можете добавить его с
rails g migration AddExpenseTypeToExpense expense_type_id:integer
А потом rake db:migrate
То же относится и к Income и IncomeType