对于编程和编程社区来说真的很新,这只是我用ruby进行的第二个项目编程,所以如果我错过了一些明显或做错的话,请原谅。
到目前为止,在这个项目中,我尝试使用Datamapper创建数据库。数据库应包含三个对象:User,Category和Class。用户可以拥有多个类别,并且类别可以属于许多用户,这就是我将这两个对象与UserCategory链接以创建多对多关系的原因。另一个关联是Category和Class之间的关系,即Category对象可以有许多类,但是一个类只能属于一个Category。到目前为止,这是我的数据库:
require "sinatra"
require "data_mapper"
if ENV['RACK_ENV'] == 'production'
DataMapper.setup(:default, ENV['DATABASE_URL'])
else
DataMapper.setup(:default, "sqlite:online-arena.db")
end
class User
include DataMapper::Resource
property :id, Serial
property :username, String,
:required => true,
:unique => true
property :password, BCryptHash, :required => true
validates_confirmation_of :password
attr_accessor :password_confirmation
validates_length_of :password_confirmation, :min => 6
def valid_password?(unhashed_password)
self.password == unhashed_password
end
property :admin, Boolean, default: false
has n, :user_categories
has n, :categories, through: :user_categories
end
class UserCategory
include DataMapper::Resource
property :id, Serial
belongs_to :user
belongs_to :category
end
class Category
include DataMapper::Resource
property :id, Serial
property :title, String
has n, :class
has n, :user_categories
has n, :users, through: :user_categories
end
class Class
include DataMapper::Resource
property :id, Serial
property :name, String
property :teacher, String
property :spots, Integer
belongs_to :category
end
DataMapper.finalize
DataMapper.auto_upgrade!
这些是我用于此项目的宝石:
source "https://rubygems.org"
gem "sinatra"
gem "data_mapper"
gem "bcrypt"
gem "rake"
group :development do
gem "sqlite3"
gem "dm-sqlite-adapter"
gem "dotenv"
gem "rerun"
end
group :production do
gem "pg"
gem "dm-postgres-adapter"
end
我使用Cloud9来处理这个项目,到目前为止,我已经"捆绑安装 - 没有生产",然后使用" ruby online-arena.rb启动它-p $ PORT -o $ IP"但它给了我这个错误:
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/dm-core-1.2.1/lib/dm-core/property/numeric.rb:19:in `fetch': key not found: :precision (KeyError)
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/dm-core-1.2.1/lib/dm-core/property/numeric.rb:19:in `initialize'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/dm-core-1.2.1/lib/dm-core/property/integer.rb:15:in `initialize'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/dm-core-1.2.1/lib/dm-core/model/property.rb:55:in `new'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/dm-core-1.2.1/lib/dm-core/model/property.rb:55:in `property'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/dm-validations-1.2.0/lib/dm-validations/auto_validate.rb:11:in `property'
from /home/ubuntu/workspace/models.rb:49:in `<class:Class>'
from /home/ubuntu/workspace/models.rb:46:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121:in `require'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121:in `require'
from /home/ubuntu/workspace/environment.rb:5:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121:in `require'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121:in `require'
from online-arena.rb:4:in `<main>'
如果没有足够的信息,请访问我的github上的其他文件 https://github.com/FuriousPenguins/Online-Arena
我尝试使用我之前项目的具有类似布局的数据库模型:
https://github.com/kichoy/lunch-reviews/blob/master/models.rb
我能够运行该应用程序。