在Ruby on Rails中创建一个名为type的字段

时间:2014-12-02 16:05:45

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我遇到问题我在ROR中创建了以下模型

  create_table "seat_types", :force => true do |t|
    t.string   "type"
    t.string   "description"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

但是当我在rails控制台中向模型添加数据时,会出现以下错误 字段类型是字符串而不是外键

1.9.3-p547 :014 > SeatType
 => SeatType(id: integer, type: string, description: string, created_at: datetime, updated_at: datetime) 
1.9.3-p547 :015 > a=SeatType.new
 => #<SeatType id: nil, type: nil, description: nil, created_at: nil, updated_at: nil> 
1.9.3-p547 :016 > a.type="SC"
 => "SC" 

1.9.3-p547 :017 > a.description="Servicio semicama"
 => "Servicio semicama" 
1.9.3-p547 :018 > a.save
   (0.2ms)  BEGIN
  SQL (0.5ms)  INSERT INTO `seat_types` (`created_at`, `description`, `type`, `updated_at`) VALUES ('2014-12-02 16:02:37', 'Servicio semicama', 'SC', '2014-12-02 16:02:37')
   (43.7ms)  COMMIT
 => true 
1.9.3-p547 :019 > a
 => #<SeatType id: 4, type: "SC", description: "Servicio semicama", created_at: "2014-12-02 16:02:37", updated_at: "2014-12-02 16:02:37"> 
1.9.3-p547 :020 > SeatType.all
  SeatType Load (0.5ms)  SELECT `seat_types`.* FROM `seat_types` 
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'A'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite SeatType.inheritance_column to use another column for that information.
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/inheritance.rb:143:in `rescue in find_sti_class'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/inheritance.rb:136:in `find_sti_class'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/inheritance.rb:62:in `instantiate'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/querying.rb:38:in `block (2 levels) in find_by_sql'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/querying.rb:38:in `collect!'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/querying.rb:38:in `block in find_by_sql'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/explain.rb:41:in `logging_query_plan'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/querying.rb:37:in `find_by_sql'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/relation.rb:171:in `exec_queries'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/relation.rb:160:in `block in to_a'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/explain.rb:34:in `logging_query_plan'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/relation.rb:159:in `to_a'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/relation/finder_methods.rb:159:in `all'
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/querying.rb:5:in `all'
    from (irb):20
    from /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/railties-3.2.19/lib/rails/commands/console.rb:47:in `start'

1 个答案:

答案 0 :(得分:5)

type是STI机制的保留名称,如错误所示,因此您不能以这种方式命名列,除非您想使用STI。例如,将其命名为kind

参考: http://guides.rubyonrails.org/active_record_basics.html#schema-conventions