如何将新的ActiveRecord关系对象添加到空对象?

时间:2016-06-03 20:01:09

标签: ruby-on-rails ruby activerecord

现在我尝试将一个新的ActiveRecord关系对象添加到新的空ActiveRecord关系对象中,该对象是我从数据库获取的.none方法

但似乎我不知道如何做到这一点。 这是我的代码

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

然后这就是我从服务器日志中得到的。

are y rady
i am cote
#<CustomerPrbInsurance id: 396, first_name: "Adonis", last_name: "Considine", tel_number: "1-171-422-3411", email: "cortez@mcclurehickle.net", 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

那么如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

似乎你有顶级的复杂关系服务代码,选择所有客户尝试下面的SQL关系,暗示你有CustomerPrbInsurance属性:prb_insurance,属于PrbInsurance:< / p>

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

然后:

CustomerPrbInsurance.by_prb_type(params[:type])