验证使用Cocoon进行多态关联失败

时间:2016-08-22 04:25:58

标签: ruby-on-rails ruby polymorphic-associations cocoon-gem

我的架构中有以下模型:

String url_payment_jul = Config.PAYMENT_JUL_URL+"?reference="+Config.userInfo.get(5);
    JsonArrayRequest req_jul = new JsonArrayRequest(url_payment_jul,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response_jul) {
                    Log.d("my data", response_jul.toString());

                    try {
                        // Parsing json array response
                        // loop through each json object
                        String jsonResponse = "";
                        for (int i = 0; i < response_jul.length(); i++) {

                            JSONObject paymentInfo_jul = (JSONObject)response_jul.get(i);

                            Config.paymentInfo_jul.add( paymentInfo_jul.getString("amount"));// Amount_jul in index (0)
                            Config.paymentInfo_jul.add( paymentInfo_jul.getString("vat"));// VAT_jul in index (1)
                            Config.paymentInfo_jul.add( paymentInfo_jul.getString("status"));// Status_jul in index (2)
                        }

                        String status_jul = Config.paymentInfo_jul.get(2);

                        if (status_jul == null){
                            textView_status_jul.setText(" Not Paid");

                        }else {
                            fixedAmount_jul();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(",y error", "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

每当我尝试保存通知时,我都会收到错误消息 - class Notification < ApplicationRecord has_many :impacts, as: :impactable accepts_nested_attributes_for :impacts, reject_if: :all_blank, allow_destroy: true end class Impact < ApplicationRecord #association belongs_to :impactable, polymorphic: true #enum enum impact_type: [:full_school, :standard, :section] end

我尝试使用Validation failed: Impacts impactable must exist手动创建通知,但效果很好。

这可能是什么问题?

更多信息 - 当我在@notification对象中添加一个byebug之前它保存在控制器中 - 这是输出 -

Notification.last.impacts.create

并检查它的关联 -

>> @notification
=> #<Notification id: nil, notification_type: "email", title: "Test", content: "Tomorrow is a holiday", created_at: nil, updated_at: nil>

1 个答案:

答案 0 :(得分:2)

您只需要在通知模型中添加28h

inverse_of: :impactable