true do |t| t.string "type" t.s-6ren">
gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:33:06 27 4
gpt4 key购买 nike

我在 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 console 中向模型添加数据时,出现以下错误字段类型是字符串不是外键

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'

最佳答案

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

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

关于ruby-on-rails - 在 Ruby on Rails 中创建一个名为 type 的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27253805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com