Tag: datamapper

使用Postgresql在Datamapper中像(ilike)一样不区分大小写

我们在Sinatra应用程序中使用Datamapper,并希望使用不区分大小写,就像在Sqlite(本地开发)和Postgresql(在生产中的Heroku上)一样。 我们有这样的陈述 TreeItem.all(:name.like =>”%#{term}%”,:unique => true,:limit => 20) 如果term是“BERL”,我们会从Sqlite和Postgresql后端获得建议“BERLIN”。 但是,如果term是“Berl”,我们只从Sqlite而不是Postgresql获得结果。 我想这与dm-postgres-adapter和dm-sqlite-adapter在生成的SQL查询中输出LIKE这一事实有关。 由于Postgresql具有区分大小写的LIKE我们得到了这个(对于我们不需要的)行为。 有没有办法像Datamapper一样不区分大小写而不诉诸于对适配器使用原始SQL查询或修补适配器以使用ILIKE而不是LIKE ? 我当然可以在两者之间使用某些东西,例如: TreeItem.all(:conditions => [“name LIKE ?”,”%#{term}%”],:unique => true,:limit => 20) 但是我们将在我们自己的代码中使用Postgresql,而不仅仅是作为适配器的配置。

Datamapper:通过关联对结果进行排序

我正在开发一个使用Datamapper作为其ORM的Rails 3.2应用程序。 我正在寻找一种通过关联模型的属性对结果集进行排序的方法。 具体来说我有以下型号: class Vehicle include DataMapper::Resource belongs_to :user end class User include DataMapper::Resource has n, :vehicles end 现在我希望能够查询车辆并按驱动程序的名称对它们进行排序。 我尝试了以下但似乎没有使用Datamapper: > Vehicle.all( :order => ‘users.name’ ) ArgumentError: +options[:order]+ entry “users.name” does not map to a property in Vehicle > Vehicle.all( :order => { :users => ‘name’ } ) ArgumentError: +options[:order]+ entry [:users, “name”] of […]

将Datamapper与现有的rails应用程序一起使用

我有一个使用ActiveRecord的现有Rails 3应用程序,我想切换到Datamapper。 dm-rails页面中给出的说明仅涉及创建新应用程序。 有谁知道如何丢弃所有activerecord dependancies并迁移到datamapper? 谢谢!

使用DataMapper get在Rails3控制器中处理404的最佳方法

这很简单,我想通过调用DataMapper来处理正常的[show]请求,就像我在Merb中所做的那样。 使用ActiveRecord,我可以这样做: class PostsController def show @post = Post.get(params[:id]) @comments = @post.comments unless @post.nil? end end 它通过捕获资源的exception来处理404。 相反,DataMapper不会自动执行此操作,因此我现在正在使用此解决方案解决此问题:[在答案中移动] 有可能告诉控制器在not_found函数内停止吗?

使用DataMapper而不是ActiveRecord

DataMapper的想法肯定比ActiveRecord好。 它有一个用于各种数据存储的API,包括RDBMS和NoSQL存储。 DataMapper比ActiveRecord更智能。 它具有“战略急切加载”。 此function单手消除“N + 1查询问题”。 此外,它允许延迟加载像Text属性这样的重字段。 DataMapper允许您通过提供嵌套的条件哈希来创建和搜索任何复杂的对象图。 ActiveRecods不适合用于生产中的关联。 include方法很丑陋且不可配置。 2010年10月,Josh Symonds 为此方法制作了一个补丁,允许将字段排除在急切加载之外。 但是这条路被忽略了,今天在rails3中我们有同样丑陋的包含方法。 即使在具有出色AREL的Rails3中,ActiveRecord也远不是最好的轨道。 你可能会说“哇,新的gem metawhere允许编写类似Article.where的好代码Article.where(:title.matches => ‘Hello%’, :created_at.gt => 3.days.ago) ”。 但等等…… DataMapper支持这个开箱即用! 也许而不是修改ActiveRecord看起来像DataMapper更好的是扩展和支持更好的orm? 有关DataMapper的更多详细信息,请访问http://datamapper.org/ 。 我认为,后来版本中的rails应该允许我们选择使用哪个orm,就像现在它允许数据库选择一样。 即使它只有一个选项“ActiveRecord”,人们也会搜索可用的替代品。 当我开始学习rails时,我认为只支持ActiveRecord。 后来我甚至没有尝试寻找别的东西。 为什么我要写这些东西? 我想,我们需要更加关注这个漂亮的ORM。 如果您是一些流行或不那么受欢迎的gem的开发者,请考虑添加对DataMapper的支持。 DataMapper社区应该从ActiveRecord或其他ORM编写一些迁移指南,并保持文档的最新状态,您可以帮助他们。 至于我,DataMapper社区需要更多人,你可能就是其中之一。 这个ORM的唯一缺点是缺少文档,你可以提供帮助。 那你觉得怎么样?

rspec,factory_girl和datamapper的spork和cache_classes问题

我有Spork测试服务器的问题。 如果我在config / environments / test.rb中设置config.cache_classes = false,那么规格就会开始出现rasie错误。 Failure/Error: task = Factory(:something, :foo => @foo, :bar => @bar) DataMapper::ImmutableError: Immutable resource cannot be modified 这是我的spec_helper.rb: require ‘spork’ Spork.prefork do if ENV[‘CODE_COVERAGE’] == ‘1’ require ‘simplecov’ SimpleCov.start ‘rails’ end ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ‘webmock/rspec’ require ‘factory_girl’ Dir[Rails.root.join(“spec/controllers/shared/*.rb”)].each { |f| require f […]

无法在Windows上加载此类文件 – do_sqlite3 / 2.0 / do_sqlite3

据我所知,我已经正确安装了所有东西,但是我仍然无法运行我的程序。 要清楚,这是在linux上编写的,我现在正试图在Windows机器上运行它。 我有以下gem清单: *** LOCAL GEMS *** addressable (2.2.7, 2.2.6) akami (1.2.2) backports (3.6.0) bcrypt (3.1.7 x64-mingw32) bcrypt-ruby (3.1.5 x64-mingw32) bigdecimal (1.2.0) builder (3.2.2, 3.1.4) bundle (0.0.1) bundler (1.6.2) daemons (1.1.9) data_mapper (1.2.0) data_objects (0.10.14) dm-aggregates (1.2.0) dm-constraints (1.2.0) dm-core (1.2.0) 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) […]

Ruby Datamapper .count始终返回0

每当我尝试从datamapper计算返回的记录时,它总是返回0,无论是否有用户。 User.count(:username=>params[:username]) class User include DataMapper::Resource property :id, Serial property :username, String, unique_index: true, required: true, length: 3..32 property :password, String, required: true, length: 5..64 property :email, String, unique_index: true, required: true, format: :email_address end

使用ruby和datamapper检索具有多对多关联的记录

我正在学习Sinatra,我已经阅读了datamapper文档并找到了这个n到n的关系示例: class Photo include DataMapper::Resource property :id, Serial has n, :taggings has n, :tags, :through => :taggings end class Tag include DataMapper::Resource property :id, Serial has n, :taggings has n, :photos, :through => :taggings end class Tagging include DataMapper::Resource belongs_to :tag, :key => true belongs_to :photo, :key => true end 我从上面的代码中理解的是,一张照片可能有很多或零标签,而标签可能有很多或零照片。 如何检索已加载相关标签的照片列表。 我知道datamapper使用惰性方法,因此它不会自动加载关联的类(在本例中为photo.tag)。 所以这: photos […]

使用RSpec测试Datamapper模型

我正在使用RSpec测试一个使用DataMapper的Sinatra应用程序。 以下代码: it “should update the item’s title” do lambda do post “/hello/edit”, :params => { :title => ‘goodbye’, :body => ‘goodbye world’ } end.should change(Snippet, :title).from(‘hello’).to(‘goodbye’) end 导致此错误: title应该最初是“hello”,但是#DataMapper :: Property :: String @ model = Snippet @ name =:title> 我当然可以通过删除lambda来解决这个问题,并且只检查是否: Snippet.first.title.should == ‘goodbye’ 但这不是一个长期解决方案,因为.first Snippet在未来可能不一样。 有人能告诉我正确的语法吗? 谢谢。