【问题标题】:Can't use conditions on symbols. DataMapper with Sinatra不能对符号使用条件。带有 Sinatra 的 DataMapper
【发布时间】:2015-05-12 09:01:35
【问题描述】:

我在 DataMapper 中尝试对符号使用条件时遇到了麻烦:

 clusters = Cluster.all(:latitude.not => nil)

此代码抛出错误:

ArgumentError - condition #<Origin::Key:0x50d05a0 @name=:latitude, @strategy=:__override__, @operator="$not", @expanded=nil, @block=
nil> of an unsupported object Origin::Key:
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:888:in `block in assert_valid_conditions'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:853:in `each'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:853:in `assert_valid_conditions'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:776:in `block in assert_valid_options'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:766:in `each'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:766:in `assert_valid_options'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:363:in `update'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/query.rb:386:in `merge'
        C:/Ruby21/lib/ruby/gems/2.1.0/gems/dm-core-1.2.1/lib/dm-core/model.rb:767:in `scoped_query'

集群模型类:

class Cluster
  include DataMapper::Resource

  property :id,               Serial
  property :city_id,          Integer
  property :name,             String
  property :latitude,         Float
  property :longitude,        Float      
end

我正在使用的核心 gem 版本:sinatra - 1.4.6、data_mapper - 1.2.0。

这段代码有什么问题?无法解决此错误。

【问题讨论】:

    标签: ruby sinatra datamapper


    【解决方案1】:

    你可以这样做:

    clusters = Cluster.all - Cluster.all(:latitude => nil) 
    

    Reference(组合查询13)

    【讨论】:

    • 是的,它有效。此行是否执行 2 个查询?不要认为这是一个非常优雅的解决方案。但是,它仍然有效,谢谢。
    • @Antrille 是的,不幸的是它执行了 2 个查询。这很奇怪.not 对你不起作用,但这是我想出的唯一解决方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多