Home > Mobile >  how append an object to association relation in rails?
how append an object to association relation in rails?

Time:10-20

In a rails 4.1 application I need to add an object to an "AssociationRelation"

  def index
    employee = Employee.where(id_person: params[:id_person]).take
    receipts_t = employee.receipts.where(:consent => true) #gives 3 results 
    receipts_n = employee.receipts.where(:consent => nil).limit(1) #gives 1 result

    #I would need to add the null consent query result to the true consent results
    #something similar to this and the result is still an association relation
    @receipts = receipts_t   receipts_n

  end

Is there a simple way to do this?

CodePudding user response:

A way of solving this:

  def index
    employee_receipts = Employee.find_by(id_person: params[:id_person]).receipts
    receipts_t = employee_receipts.where(consent: true)
    receipts_n = employee_receipts.where(consent: nil).limit(1)

    @receipts = Receipt.where(id: receipts_t.ids   receipts_n.ids)
  end

CodePudding user response:

you could do this way

receipts_t_ids = employee.receipts.where(:consent => true).pluck(:id)
receipts_n_ids = employee.receipts.where(:consent => nil).limit(1).pluck(:id)

@receipts = Receipt.where(id: receipts_t_ids   receipts_n_ids)
  • Related