有没有人知道在Heroku的Bamboo堆栈上运行使用DataMapper的Sinatra应用程序所需的神奇咒语?Bamboo堆栈不包含任何预先安装的系统宝石,无论我尝试什么样的宝石组合,我都会遇到此错误:
undefined method `auto_upgrade!' for DataMapper:Module (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
这就是我在我的.gems文件中所拥有的:
sinatra
pg
datamapper
do_postgres
dm-postgres-adapter
Run Code Online (Sandbox Code Playgroud)
这些是我将应用程序推送到Heroku时安装的依赖项:
-----> Heroku receiving push
-----> Sinatra app detected
-----> Installing gem sinatra from http://rubygems.org
Successfully installed sinatra-1.0
1 gem installed
-----> Installing gem pg from http://rubygems.org
Building native extensions. This could take a while...
Successfully installed pg-0.9.0
1 gem installed
-----> Installing gem datamapper from http://rubygems.org
Successfully installed extlib-0.9.15
Successfully installed addressable-2.2.1
Successfully installed dm-core-1.0.2
Successfully installed dm-aggregates-1.0.2
Successfully installed dm-migrations-1.0.2
Successfully installed dm-constraints-1.0.2 …Run Code Online (Sandbox Code Playgroud) 我有一个Ruby程序,它使用DataMapper作为ORM来与内存中的SQLite DB进行通信.这一直很好,但我刚刚添加了一个新的DM类和相应的表.令我惊讶的是,在auto_migrate期间,事情现在爆炸了!
这是DataMapper生成的SQL:
~ (0.000390) PRAGMA table_info("sensationd_channels")
~ (0.000010) PRAGMA table_info("sensationd_commands")
~ (0.000009) PRAGMA table_info("sensationd_configurations")
~ (0.000052) PRAGMA table_info("sensationd_measurements")
~ (0.000028) SELECT sqlite_version(*)
~ (0.000035) DROP TABLE IF EXISTS "sensationd_channels"
~ (0.000009) PRAGMA table_info("sensationd_channels")
~ (0.000423) CREATE TABLE "sensationd_channels" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "channel" INTEGER NOT NULL, "name" VARCHAR(50), "precision" INTEGER DEFAULT 11, "gain" INTEGER DEFAULT 1, "differential" BOOLEAN DEFAULT 'f', "configuration_id" INTEGER NOT NULL)
~ (0.000191) CREATE INDEX "index_sensationd_channels_configuration" ON "sensationd_channels" ("configuration_id")
~ (0.000015) DROP TABLE …Run Code Online (Sandbox Code Playgroud) 作为一个学习练习,我正在使用Sinatra,Datamapper和RSpec构建练习应用程序.我正在使用这个模板,它基本上是上述所有的样板.
我遇到的问题是RSpec和Datamapper似乎配置的方式,每次运行测试数据库相关功能的规范时,这些测试直接更改我的开发数据库而不是测试数据库.
例如,我希望在运行其余规范之前创建一些数据库对象...
before(:all) {
Region.new(:country => "Canada", :country_code => "CA").save
ProductLine.new(:reference => "Molders").save
Product.new(:name => "The Black Molder").save
Cart.new(:price => 25.95).save
}
Run Code Online (Sandbox Code Playgroud)
然而,每次运行RSpec时,上述元素都会添加到我的开发数据库中.
为什么不生成测试数据库呢?如何让测试数据库正常工作?
除了Sinatra而不是Rails之外,这看起来与此问题非常类似.
如果有任何帮助,我的代码可以在这里看到.
我在codeigniter的控制器中使用以下查询.
$u -> where('us_email_id', $username);
$u -> where('us_password', $password1);
$details = $u -> get();
$total = count($details);
echo $total; echo "<br>";
echo count($details);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,"$ u"是数据映射器"User"的类"User"的对象名,其中我的数据库中的表名是"users".我想看看执行查询后返回了多少行.即使用户ID和密码不匹配,"$ total"也始终显示1.我想要的是,如果行数返回1则"ok"否则"出错".我知道它的基本但如果有人知道那么请帮助我.提前致谢.
php codeigniter datamapper codeigniter-2 codeigniter-datamapper
我正在开发一个 Sinatra 应用程序,我想在其中使用 data_mapper 和 sqlite3。在 app.rb 我有:
require 'sinatra'
require 'data_mapper'
Run Code Online (Sandbox Code Playgroud)
在控制台中,当我执行 $ gem list 我得到(list 稍微编辑以反映 db gems:
* LOCAL GEMS *
activemodel (4.0.2)
activerecord (4.0.2)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.2)
addressable (2.3.5)
arel (4.0.2)
atomic (1.1.14)
bcrypt-ruby (3.1.2)
bigdecimal (1.2.3)
buftok (0.2.0)
builder (3.1.4)
bundler (1.5.3)
bundler-unload (1.0.2)
cookiejar (0.3.0)
daemons (1.1.9)
data_mapper (1.2.0)
data_objects (0.10.13)
descendants_tracker (0.0.3)
dm-aggregates (1.2.0)
dm-constraints (1.2.0)
dm-core (1.2.1)
dm-do-adapter (1.2.0)
dm-migrations (1.2.0)
dm-serializer (1.2.2)
dm-sqlite-adapter (1.2.0)
dm-timestamps (1.2.0)
dm-transactions (1.2.0)
dm-types (1.2.2) …Run Code Online (Sandbox Code Playgroud) class Item
include DataMapper::Resource
property :id, Serial
property :title, String
end
item = Item.new(:title => 'Title 1') # :id => 1
item.save
item_clone = Item.first(:id => 1).clone
item_clone.save
# => <Item @id=1 @title="Title 1" ...
Run Code Online (Sandbox Code Playgroud)
这确实如所描述的那样"克隆"对象但是如何这样做以便在保存记录时应用不同的ID,例如
# => <Item @id=2 @title="Title 1" ...
Run Code Online (Sandbox Code Playgroud) 无法理解为什么钩子不起作用.我有以下型号:
class DirItem
include DataMapper::Resource
# property <name>, <type>
property :id, Serial
property :dir_cat_id, Integer, :required => true
property :title, String, :required => true
property :price, Integer, :default => 0
belongs_to :dir_cat
has n, :dir_photos
has n, :dir_field_values
before :destroy do
logger.debug "==============DESTROYING ITEM ##{id}, TITLE
#{title}"
dir_field_values.destroy
dir_photos.destroy
end
end
Run Code Online (Sandbox Code Playgroud)
当我destroy从我的应用程序或irb 调用方法时,它返回false.该errors哈希为空,则日志消息不打印和记录将不会删除.
我正在使用DataMapper并尝试使用模型Project和Task之间的关联.我将模型放在单独的文件project.rb和task.rb中.当我尝试将它们彼此关联时,我收到以下错误:
Cannot find the parent_model Project for Task in project (NameError)
我收集这是由project.rb引起的,需要task.rb,反之亦然,因为如果我将它包含在其中一个文件中,则关联工作正常.这是代码:
project.rb
require 'dmconfig'
require 'task'
class Project
include DataMapper::Resource
property :id, Serial
has n, :tasks
end
DataMapper.auto_upgrade!
DataMapper.finalize
Run Code Online (Sandbox Code Playgroud)
task.rb
require 'dmconfig'
require 'project'
class Task
include DataMapper::Resource
property :id, Serial
belongs_to :project
end
DataMapper.auto_upgrade!
DataMapper.finalize
Run Code Online (Sandbox Code Playgroud)
dmconfig.rb
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite://' + Dir.pwd + '/taskmanager.db')
Run Code Online (Sandbox Code Playgroud)
如果我从其中一个文件中删除关联它工作正常,至少从一个方向:
require 'dmconfig'
class Project
include DataMapper::Resource
property :id, Serial
end
DataMapper.auto_upgrade!
DataMapper.finalize
Run Code Online (Sandbox Code Playgroud)
如果我希望关联从两个方向工作是唯一合理的解决方案,只是将两个类放在同一个文件中?或者有没有办法让他们分开并仍然管理它?
我和Limpens先生有一个非常相似的问题,其中一个主要不同:我确实将test_unit railtie包含在我的application.rb中.
从我的application.rb:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require 'action_controller/railtie'
require 'dm-rails/railtie'
# require 'action_mailer/railtie'
# require 'active_resource/railtie'
require 'rails/test_unit/railtie'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
您将注意到我正在使用DataMapper并使用dm-rails引导程序初始化我的项目.我可以手动运行我的测试,如下所示:
$ ruby -Itest test/unit/test_habit.rb Loaded suite test/unit/test_habit Started ..... Finished in 2.554523 seconds. 5 tests, 7 assertions, 0 failures, …
我有一个数据库,其中包含需要操作的行的列表。看起来像这样:
id remaining delivered locked
============================================
1 10 24 f
2 6 0 f
3 0 14 f
Run Code Online (Sandbox Code Playgroud)
我将DataMapper与Ruby结合使用,但实际上我认为这是一个通用的编程问题,并不特定于我正在使用的确切实现...
我正在创建一堆工作线程,它们执行以下操作(伪红代码):
while true do
t = any_row_in_database_where_remaining_greater_than_zero_and_unlocked
t.lock # update database to set locked = true
t.do_some_stuff
t.delivered += 1
t.remaining -= 1
t.unlock
end
Run Code Online (Sandbox Code Playgroud)
当然,问题是,这些线程相互竞争,并且整个线程并不是真正安全的。while循环中的第一行可以轻松地在多个线程中拉出同一行,然后才有机会被锁定。
我需要确保一个线程只能同时在一行上工作。
做这个的最好方式是什么?
datamapper ×10
ruby ×7
sinatra ×4
sqlite ×2
associations ×1
clone ×1
codeigniter ×1
database ×1
duplicates ×1
heroku ×1
hook ×1
php ×1
postgresql ×1
rake ×1
records ×1
rspec ×1
shotgun ×1
testing ×1