String数组上的Ruby子类错误

时间:2013-11-14 14:55:46

标签: ruby

我确定我的班级出了问题,但现在是:

class Transaction < ActiveRecord::Base
      attr_accessible :transaction_date, :amount, :other_info, :type, :purchase
end

require 'csv'
require_relative '../../app/models/transaction'
csv_text = File.read('monthly_csvs/pcbanking.csv')
csv = CSV.parse(csv_text, :headers => false)
csv.each do |row|
  puts row[3].to_s
  Transaction.create!(transaction_date: row[0], amount: row[1], other_info: row[2], type: row[3], purchase: row[4])
end

ERROR:

POS Purchase
rake aborted!
Invalid single-table inheritance type: POS Purchase is not a subclass of Transaction

Pos Purchase是row [3]元素,是一个字符串。

1 个答案:

答案 0 :(得分:1)

Rails(或更具体地说,ActiveRecord)在模型中默认使用type列来实现Single Table Inheritance(STI)。这是一种实现多个继承模型的技术,这些模型保存在同一个数据库表中。

当您在模型中使用type列时,Rails希望它用于STI。您现在可以将type列重命名为其他内容,或者指示Rails使用另一列作为STI类型列,方法是在模型类中使用此列(在此示例中为sti_type列):

class Transaction < ActiveRecord::Base
  self.inheritance_column = :sti_type
end