I am trying to create an array of ids so that I can query if a certain id is associated with an object. Here is my method but it isnt adding id's to the array.
class Report
include Mongoid::Document
has_and_belongs_to_many :reportapprovals, class_name: "Reportapproval", inverse_of: :report
def bind_reportapproval
@reportapprovals = Reportapproval.where(tenant_id: self.tenant_id).all
if @reportapprovals.present? && @reportapprovals.any? { |ra| ra.tenant_approved == true }
@reportapprovals.each do |ra|
self.reportapproval_ids = ra.id
end
end
end
end
This is suppose to add an array of reportapproval_ids to the report object.
ifstatement you've got this:@reportapprovals.any? { |tenant_approved| tenant_approved == true}which is going to iterate through eachreportapprovaland check if it is equal totrue, but it won't ever be since it will be an instance of a Mongoid record. Did you maybe mean something like this?@reportapprovals.any? { |ra| ra.tenant_approved == true}