-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 7.2.0, 7.0.11, 7.1.6
-
Component/s: Associations, Query
-
None
Setup:
class Company include Mongoid::Document field :legacy_id, type: Integer has_many :products, inverse_of: :company, primary_key: 'legacy_id', foreign_key: 'company_legacy_id' end class Product include Mongoid::Document field :legacy_id, type: Integer field :company_legacy_id, type: Integer belongs_to :company, inverse_of: :products, primary_key: 'legacy_id', foreign_key: 'company_legacy_id' end
This works correctly:
c = Company.create!(legacy_id: 3) # => #<Company _id: 60069b5158af1454d9ae1818, legacy_id: 3> p = Product.create!(legacy_id: 5, company: c) # => #<Product _id: 60069b6658af1454d9ae1819, legacy_id: 5, company_legacy_id: 3>
However this query ignores primary key (AR works correctly in such cases):
Product.where(company: c) # => #<Mongoid::Criteria # selector: {"company_legacy_id"=>BSON::ObjectId('60069b5158af1454d9ae1818')} # options: {} # class: Product # embedded: false>
Foreign key is correct, but _id is used as value ignoring custom primary_key.