مُستكشف البيانات `int_list` يستبدل العنصر الأول بالصفر

لا أملك يقينًا إن كان هذا خطأً برمجيًا أم أنني أقوم بشيء خاطئ، لكن يبدو أن العنصر الأول في مصفوفة int_list يُستبدل دائمًا بالصفر في استعلام مستكشف البيانات.

على سبيل المثال:

-- int_list :categories = "3, 5, 6"

يتحول إلى [0, 5, 6]

عند النظر في:

إذا قمت بذلك في وحدة التحكم (console)، أحصل على النتيجة المتوقعة:

string = "3,5,6"
value = string.split(',').map { |s| s.downcase == '#null' ? nil : s.to_i }
=> [3, 5, 6]

يمكنك إعادة إنتاج المشكلة باستخدام هذا الاستعلام:

-- [params]
-- int_list :categories = "3, 5, 6"

SELECT *
FROM topics 
WHERE topics.category_id = ANY (ARRAY [ :categories ]  )

لإحداث الخطأ (بحيث يمكنك رؤية رسالة الخطأ)، ما عليك سوى إزالة القوس المغلق، على سبيل المثال:

-- [params]
-- int_list :categories = "3, 5, 6"

SELECT *
FROM topics 
WHERE topics.category_id = ANY (ARRAY [ :categories   )
PG::SyntaxError: ERROR:  syntax error at or near ")"
LINE 12: WHERE topics.category_id = ANY (ARRAY [ 0,5,6   )
                                                         ^

(انظر إلى المصفوفة)

إعجاب واحد (1)

It looks like what is happening when you set a default value for an int_list parameter in this way:

-- int_list :categories = "3, 5, 6"

is that the following gets run:

'"1'.downcase.to_i

That will return 0.

You can get around the issue by leaving the quotes off the default parameter:

-- int_list :categories = 3, 5, 6

That value still gets interpreted as a string before being split into an array. Maybe the plugin should strip outer quotation marks if they get added to a string input.

6 إعجابات

Thanks Simon! :smiley:

I was sure I had tried that… and looking again now I see that changing default parameters in the body and clicking on save and run has no effect - you either have to refresh the page after saving or change the values in the box for the field as well (which I totally forgot about).

إعجابَين (2)

FWIW I find params really fiddly to reason about, I wonder if we should shift this to full UX vs magic comments @riking

5 إعجابات

Yup, totally makes sense to do that, the magic comments were optimizing for minimum # of plugin-sourced DB migrations, which are much less of a problem these days.

5 إعجابات

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.