mysql - Rails find_by with OR -
i have named scope set in rails application used locate record either id (directly index view) or uuid (from email - users can't enter in id , view record)
scope :by_uuid, lambda { |id| where('id = ? or uuid = ?', id, id) }
this used in show action, id comes url, like
services/114 services/74c083c0-8c29-012f-1c87-005056b42f8a
this works great, until uuid such 74c083c0-8c29-012f-1c87-005056b42f8a this, rails unfortunately converts int value , services/74 record correct uuid
adding .first scope not because order different each record, not work.
is there way prevent rails converting id , taking string literally? obviously, there not record id matches that, if goes willy-nilly integer values of string passed it, back.
using dynamic finders, such
service.find_by_uuid
or
service.find_by_id
work intended, need able retrieve record using uuid or id in same method (show).
is there way like
service.find_by_id_or_uuid
we fixed issue following change scope:
scope :by_uuid, lambda { |id| where('binary id = ? or uuid = ?', id, id) }
this ensures id string taken binary value instead of being converted int. however, only work mysql based apps
Comments
Post a Comment