-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: Associations
This works;
class Contract include Mongoid::Document has_and_belongs_to_many :signatures field :item, type: String end class Signature include Mongoid::Document has_and_belongs_to_many :contracts field :name, type: String field :year, type: Integer end c=Contract.create! s=Signature.create!(contracts:[c]) s.contracts.first # => #<Contract _id: 5c5dda8e026d7c1730969846, signature_ids: [BSON::ObjectId('5c5dda9a026d7c1730969847')], item: nil>
Note that the contract got the signature added to its signature_ids.
Now, if both classes have class_name set, contract does not get the signature id added to its signature_ids:
class HabtmmContract include Mongoid::Document has_and_belongs_to_many :signatures, class_name: 'HabtmmSignature' field :item, type: String end class HabtmmSignature include Mongoid::Document has_and_belongs_to_many :contracts, class_name: 'HabtmmContract' field :name, type: String field :year, type: Integer end contract = HabtmmContract.create!(item: 'foo') s=HabtmmSignature.create!(contracts: [contract], name: 'Dave', year: 2019) s.contracts.first # => #<HabtmmContract _id: 5c5dda46026d7c1730969844, signature_ids: nil, item: "foo">
One therefore has to do:
contract = HabtmmContract.create!(item: 'foo') contract.signatures << HabtmmSignature.create!(contracts: [contract], name: 'Dave', year: 2019) contract.save!