我在Oracle 12c数据库的表中有一个名为 type_name 的列,每当我尝试使用Rails控制台添加记录时,我收到以下消息。 我应该将列名称从 type_name 更改为任何其他名称以解决此问题吗?
型号:
class LicenseTypes < ActiveRecord::Base
validates :name, :type, presence: true
def as_json(options = {})
{
id: id,
name: name,
description: description,
type_name: type_name
}
end
end
迁移:
class CreateLicenseTypes < ActiveRecord::Migration
def change
create_table :license_types do |t|
t.string :name
t.string :type_name
t.string :description
t.integer :created_at
t.integer :updated_at
end
end
end
代码:
LicenseType.create({name: "Car Lic" , type_name: "Car"})
错误:
NoMethodError: undefined method `type' for #<LicenseType:0x76b49d0>
from org/jruby/RubyBasicObject.java:1633:in `method_missing'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activemodel-4.2.7/lib/active_model/attribute_methods.rb:433:in `method_missing'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activemodel-4.2.7/lib/active_model/validator.rb:149:in `block in validate'
from org/jruby/RubyArray.java:1734:in `each'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activemodel-4.2.7/lib/active_model/validator.rb:148:in `validate'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activerecord-4.2.7/lib/active_record/validations/presence.rb:5:in `validate'
from org/jruby/RubyKernel.java:1808:in `public_send'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:455:in `block in make_lambda'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:192:in `block in simple'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:504:in `block in call'
from org/jruby/RubyArray.java:1734:in `each'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:504:in `call'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:778:in `_run_validate_callbacks'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activemodel-4.2.7/lib/active_model/validations.rb:399:in `run_validations!'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activemodel-4.2.7/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
... 15 levels...
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activerecord-4.2.7/lib/active_record/transactions.rb:286:in `block in save'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activerecord-4.2.7/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activerecord-4.2.7/lib/active_record/transactions.rb:285:in `save'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/activerecord-4.2.7/lib/active_record/persistence.rb:34:in `create'
from (irb):12:in `<eval>'
from org/jruby/RubyKernel.java:995:in `eval'
from org/jruby/RubyKernel.java:1296:in `loop'
from org/jruby/RubyKernel.java:1115:in `catch'
from org/jruby/RubyKernel.java:1115:in `catch'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/railties-4.2.7/lib/rails/commands/console.rb:110:in `start'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/railties-4.2.7/lib/rails/commands/console.rb:9:in `start'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/railties-4.2.7/lib/rails/commands/commands_tasks.rb:68:in `console'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/railties-4.2.7/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/jruby-9.1.5.0/lib/ruby/gems/shared/gems/railties-4.2.7/lib/rails/commands.rb:17:in `<main>'
from org/jruby/RubyKernel.java:956:in `require'
from bin/rails:4:in `<main>'
答案 0 :(得分:1)
将type
更改为type_name
,因为类型只能用于STI。
validates :name, :type, presence: true
到
validates :name, :type_name, presence: true