2016-06-03 12 views
0

Jetzt habe ich versucht, ein neues Active Beziehung Objekt hinzufügen, dass ich aus der Datenbank auf die neue leere Active Beziehung Objekt bekommen, dass ich esWie fügen Sie ein neues ActiveRecord-Beziehungsobjekt einem leeren hinzu?

von .none Methode gemacht, aber es scheint, als ob ich es nicht über die Art und Weise kennen zu tun . Dies ist mein Code

def customer_prb_insurance_table 
    @customer_prb_insurance_array = CustomerPrbInsurance.none 
    @prb_type = params[:type] 
    @prb_insurance_objects = PrbInsurance.where(prb_type: params[:type]) 

    @prb_insurance_objects.each do |prb_insurance_object| 
     prb_customers = prb_insurance_object.customer_prb_insurances 

     prb_customers.each do |prb_customer| 
     puts "are y rady" 
     puts @customer_prb_insurance_array 
     puts "i am cote" 
     puts prb_customer.inspect 
     puts "yolololololo" 

     @customer_prb_insurance_array << prb_customer 

     puts @customer_prb_insurance_array 

     byebug 

     end 
    end 

    @customer_prb_insurance_array = @customer_prb_insurance_array.paginate(:page => params[:page]) 

    end 

Dann ist das, was ich aus dem Server-Log einsehen.

are y rady 
i am cote 
#<CustomerPrbInsurance id: 396, first_name: "Adonis", last_name: "Considine", tel_number: "1-171-422-3411", email: "[email protected]", time_call_back: "evening", prb_insurance_id: 10, created_at: "2016-06-03 19:14:03", updated_at: "2016-06-03 19:14:03"> 
yolololololo 
Return value is: nil 

Also, wie das zu beheben?

Danke!

Antwort

1

Scheinen Sie oben komplexe Beziehung dient Code haben alle Kunden die follwing SQL-Beziehung versuchen zu wählen, was bedeutet, dass Sie :prb_insurance, in CustomerPrbInsurance Eigenschaft haben, die zu PrbInsurance gehört:

class CustomerPrbInsurance 
    belongs_to :prb_insurance 
    scope :by_prb_type, -> {|type| 
     joins(:prb_insurance).where(prb_insurance: { prb_type: type }) 
    } 
end 

dann:

CustomerPrbInsurance.by_prb_type(params[:type]) 
+0

@ user3403614 aktualisierte Antwort! –

Verwandte Themen