moped
moped copied to clipboard
has_many relation bad _id query reference
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
# => []
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