1

(Ruby 1.9.3、MongoDB 2.0.4、Rails 3.2、Mongoid 2.4、Geocoder 1.1.1)

私は次のモデルを持っています:

class Company
  include Mongoid::Document

  embeds_one :office

  index [[ "office.coordinates", Mongo::GEO2D ]]
end

class Office
  include Mongoid::Document
  include Geocoder::Model::Mongoid

  geocoded_by :address

  field :city,        :type => String
  field :state,       :type => String
  field :coordinates, :type => Array

  embedded_in :company

  after_validation :geocode

  def address
    "#{city}, #{state}"
  end
end

私はコンソールでこれを行います:

> c = Company.new
 => #<Company _id: 4f885aa56d20f03898000003, _type: nil> 
> c.office = Office.new(:city => "San Francisco", :state => "CA")
 => #<Office _id: 4f885ab66d20f03898000004, _type: nil, city: "San Francisco", state: "CA", coordinates: nil> 
> c.save
 => true

ここまでは順調ですね。しかし、次に、埋め込まれたドキュメント(オフィス)に対してジオクエリを実行して、会社を取得しようとします。

> Company.where(:office.near => Company.first.office.to_coordinates).first
Mongo::OperationFailure: can't find special index: 2d for: { office: { $near: [ -122.4194155, 37.7749295 ] } }
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongo-1.6.2/lib/mongo/cursor.rb:144:in `next'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongo-1.6.2/lib/mongo/collection.rb:288:in `find_one'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-2.4.8/lib/mongoid/collections/master.rb:25:in `block in find_one'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-2.4.8/lib/mongoid/collections/retry.rb:29:in `retry_on_connection_failure'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-2.4.8/lib/mongoid/collections/master.rb:24:in `find_one'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-2.4.8/lib/mongoid/collection.rb:60:in `find_one'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-2.4.8/lib/mongoid/contexts/mongo.rb:203:in `first'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-2.4.8/lib/mongoid/criteria.rb:45:in `first'
    from (irb):2
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
    from /Users/raphael/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

私は何が間違っているのですか?実行しましたrake db:mongoid:create_indexes

4

1 に答える 1

6

埋め込みフィールドに対してクエリを実行する必要があります。

Company.where(:'office.coordinates'.near => Company.first.office.to_coordinates).first

near下のジオコーダーgemによって作成されたスコープを活用する場合はOffice、次のようにすることができます。

# in Company.rb
self.near(office, radius = 20, conditions = {})
  self.where(conditions).tap do |criteria|
    near_criteria = Office.scopes[:near].conditions.call(office, radius)

    criteria.selector[:'office.coordinates'] = near_criteria.selector[:coordinates]
  end
end

これによりCompany#nearOfficeオブジェクトを取得するものが作成されます。Office#nearスコープによって作成されたクエリを会社のクエリに挿入します。

于 2012-04-13T17:53:06.143 に答える