vendredi 21 octobre 2016

rails 3.2.1 and mysql where condition with nil issue (it toggles in no record and proper result)

If I run the following query in rails console On first run it returns 7 rows and 2nd run it returns 0 rows, its keeps toggling...

@site.site_pages.published\
.where(:is_visible => true,:parent_id => nil,:lang_parent_id => nil)\
.order("position ASC")

=> [ARRAY OF RECORDS]
-- {Returns 7 rows}

@site.site_pages.published\
.where(:is_visible => true,:parent_id => nil,:lang_parent_id => nil)\
.order("position ASC")

=> []
-- {Returns 0 rows}

As a fix/work around I tried this, which worked fine, but need to know the root cause

@site.site_pages.published\
.where(:is_visible => true,:parent_id => [nil, ''],:lang_parent_id => [nil, ''])\
.order("position ASC")

NOTE: If i run the to_sql query in mysql, it works fine, so considering nothing to do with mysql version etc, but its 5.6.

Aucun commentaire:

Enregistrer un commentaire