所以我有课程'范围'由Datamapper创建(这是在Ruby / Sinatra项目中):
class Range
include DataMapper::Resource
#properties
property :id, Serial, :key => true
property :default_gateway, String
property :subnetmask, String
property :main_ip, String
property :backup_ip, String
property :dhcp, String
#relations
belongs_to :vlan, :required => true
has n, :dhcp
end #class Range
我还有一个文件' initdata'我在我的主要班级打电话。此文件的一部分尝试使用一些虚拟数据填充数据库。问题是类Ranges它给我一个ArgumentError。
Initdata ruby文件:
#CREATE RANGES
if Range.count == 0
puts '----- ADDING RANGES -----'
range1 = Range.first_or_create(:default_gateway => '10.1.2.44', :subnetmask => '0.0.255.255', :main_ip => '10.1.0.1', :backup_ip => '10.1.0.2', :dhcp => '192.168.1.2', :vlan_id => 1)
range1.errors.each do |error|
puts error
end
end
我在终端中运行时出现以下错误:
----- ADDING RANGES -----
/home/bene/.rvm/gems/ruby-2.1.3/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `initialize': wrong number of arguments (1 for 2..3) (ArgumentError)
from /home/bene/.rvm/gems/ruby-2.1.3/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `new'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `create'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/dm-core-1.2.0/lib/dm-core/model.rb:444:in `first_or_create'
from /home/bene/project/development_sam/lan_portal/dal/initdata.rb:328:in `<top (required)>'
from /home/bene/project/development_sam/lan_portal/app.rb:6:in `require'
from /home/bene/project/development_sam/lan_portal/app.rb:6:in `<top (required)>'
from /home/bene/project/development_sam/lan_portal/config.ru:1:in `require'
from /home/bene/project/development_sam/lan_portal/config.ru:1:in `block in <main>'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from /home/bene/project/development_sam/lan_portal/config.ru:in `new'
from /home/bene/project/development_sam/lan_portal/config.ru:in `<main>'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:250:in `start'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/rackup:23:in `load'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/rackup:23:in `<main>'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'
有谁知道我可能做错了什么?我已经搜索过了,但大多数解决方案都是因为人们自己写了“初始化”。函数,就像我的情况一样,它已经由DataMapper定义
答案 0 :(得分:0)
有一个核心Ruby类Range(顺便提一下,它有一个2..3参数构造函数);显然,如果构造函数已经存在,DataMapper不会重新定义构造函数。
您需要重命名您的课程。