jwood / tenacity

A database client independent way of managing relationships between models backed by different databases.
MIT License
118 stars 17 forks source link

Problems with t_has_many between ActiveRecord and Mongoid #46

Closed odcinek closed 11 years ago

odcinek commented 11 years ago

Hey,

I'm getting invalid selector selector: {"_id"=>{"$in"=>[]}} when trying to query has_many relation between AR and Mongoid. I'm not quite sure I'm using it correctly (although model layout looks just like the docs), here's my setup:

Models

class Sendout < ActiveRecord::Base
  include Tenacity
  t_has_many :recipients
end

class Recipient
  include Mongoid::Document
  include Tenacity
  t_belongs_to :sendout    
end

AR migration

class CreateSendouts < ActiveRecord::Migration
  def change
    create_table :sendouts do |t|
      t.timestamps
    end
  end
end

and finally the behaviour I'm getting

Loading development environment (Rails 3.2.13)
2.0.0p195 :001 > s=Sendout.create()
   (0.1ms)  begin transaction
  SQL (2.8ms)  INSERT INTO "sendouts" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", Sun, 16 Jun 2013 12:59:07 UTC +00:00], ["updated_at", Sun, 16 Jun 2013 12:59:07 UTC +00:00]]
   (6.7ms)  commit transaction
 => #<Sendout id: 1, created_at: "2013-06-16 12:59:07", updated_at: "2013-06-16 12:59:07">
2.0.0p195 :002 > s.recipients
 => #<Mongoid::Criteria
  selector: {"_id"=>{"$in"=>[]}}
  options:  {}
  class:    Recipient
  embedded: false>

2.0.0p195 :003 > s.save!
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
 => true
2.0.0p195 :004 > r=Recipient.new()
 => #<Recipient _id: 51bdb69b03859c3d3a000001, sendout_id: nil>
2.0.0p195 :005 > s.recipients << r
 => nil
2.0.0p195 :006 > s.recipients
 => #<Mongoid::Criteria
  selector: {"_id"=>{"$in"=>[]}}
  options:  {}
  class:    Recipient
  embedded: false>

and versions (clean rails app)

bson (1.9.0)
bson_ext (1.9.0)
mongo (1.9.0)
mongoid (3.1.4)
rails (3.2.13)
sqlite3 (1.3.7)
tenacity (0.6.0)
odcinek commented 11 years ago

Nevermind, @odcinek rtfm ;)