Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

has_many relation bad _id query reference #382

Open
sescobb27 opened this issue Jul 20, 2015 · 0 comments
Open

has_many relation bad _id query reference #382

sescobb27 opened this issue Jul 20, 2015 · 0 comments

Comments

@sescobb27
Copy link

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps::Created
  has_many :reviews
end

class Client < User
end

class Customer < User
end

class Review
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  belongs_to :customer
  belongs_to :client
end
customer = Customer.create!
# => #<Customer _id: 55ac45ce6b69724980000000, deleted_at: nil, created_at: "2015-07-20T00:50:22.649Z", _type: "Customer">
client = Client.create!
# => #<Client _id: 55ac45e46b69724980000001, deleted_at: nil, created_at: "2015-07-20T00:50:44.696Z", _type: "Client">

review = Review.create! client_id: client._id, customer_id: customer._id
# => #<Review _id: 55ac45f66b69724980000002, created_at: "2015-07-20T00:51:02.166Z", customer_id: <BSON::ObjectId:0x70076586966480 data=55ac45ce6b69724980000000>, client_id: <BSON::ObjectId:0x70076590683880 data=55ac45e46b69724980000001>>

# then
test_review = Review.find review._id
# => #<Review _id: 55ac45f66b69724980000002, created_at: "2015-07-20T00:51:02.166Z", customer_id: <BSON::ObjectId:0x70076594200940 data=55ac45ce6b69724980000000>, client_id: <BSON::ObjectId:0x70076594201040 data=55ac45e46b69724980000001>>
test_review.client
# => #<Client _id: 55ac45e46b69724980000001, deleted_at: nil, created_at: "2015-07-20T00:50:44.696Z", _type: "Client"
test_review.customer
# => #<Customer _id: 55ac45ce6b69724980000000, deleted_at: nil, created_at: "2015-07-20T00:50:22.649Z", _type: "Customer">

# BUT
Client.find('55ac45e46b69724980000001').reviews
# => []
Client.includes(:reviews).find('55ac45e46b69724980000001').reviews
# => []

Customer.find('55ac45ce6b69724980000000').reviews
# => []
Customer.includes(:reviews).find('55ac45ce6b69724980000000').reviews
# => []

image

using

gem 'mongoid', '~> 4.0.2'
gem 'mongoid_paranoia', '~> 0.1.2'
gem 'moped', '~> 2.0.6'
gem 'bson_ext'

Logs

MOPED: 127.0.0.1:27017 QUERY        database=festinare_api_development collection=reviews selector={"_id"=>{"$in"=>[<BSON::ObjectId:0x70076592330640 data=55ac45e46b69724980000001>]}} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.7984ms

selector should be client_id or customer_id, but instead is _id

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant