1

I have a problem I create the following model in 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

but when I add data to the model in rails console the following error appears the field type is a string not a foreign key

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 Answer 1

5

type is reserved name by STI mechanism, as the error says, so you can't name your column this way, unless you want to use STI. Name it kind, for example.

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

Sign up to request clarification or add additional context in comments.

1 Comment

A-a-a-and a reference as proof of this fact: guides.rubyonrails.org/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.