Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:28:33+00:00 2026-06-05T15:28:33+00:00

I have a named scope set up in my rails application that is used

  • 0

I have a named scope set up in my rails application that is used to locate a record either by its ID (directly from the index view) or a UUID (from an email – basically only so that users can’t enter in any ID and view a record)

scope :by_uuid, lambda { |id| where('id = ? OR uuid = ?', id, id) }

This is used in the show action, so the ID comes from the url, like

services/114

services/74c083c0-8c29-012f-1c87-005056b42f8a

This works great, until you get a UUID such as 74c083c0-8c29-012f-1c87-005056b42f8a
This, rails unfortunately converts to the int value and we get services/74 AS WELL AS the record with the correct UUID

Adding a .first to the scope will not help because the order could be different for each record, so that does not work.

Is there a way to prevent rails from converting the ID like this and taking the string literally? Obviously, there will not be a record with an ID that matches that, but if it goes willy-nilly with the integer values of the string passed to it, we could get anything back.

Using the dynamic finders, such as

Service.find_by_uuid

or

Service.find_by_id

work as intended, however we need to be able to retrieve a record using the UUID OR the ID in the same method (show).

Is there a way to do something like

Service.find_by_id_or_uuid
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:28:34+00:00Added an answer on June 5, 2026 at 3:28 pm

    We fixed this issue with the following change to the scope:

    scope :by_uuid, lambda { |id| where('binary id = ? OR uuid = ?', id, id) }
    

    This ensures that the id string is taken by its binary value instead of being converted into an int. However, this will ONLY WORK WITH MYSQL BASED APPS

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent class in rails that inherits from ActiveRecord::Base. I'm trying to
I have the following named scope: named_scope :report_search, lambda { |search| { :conditions =>
Our ldap is set up so that we have multiple different organizational units (ou).
I have a number of application settings (in user scope) for my custom grid
I am using devise in a rails 3 application. I have devise fully working
I want to implement application settings to have them update settings that wont be
My problem is that I have to set a variable in a try statement
I have a table named scores with the columns id and score . I
I have column named summary (tinytext, utf8_turkish_ci). I know it can store 255 byte
i have class named Group i tried to test configuration: var cfg = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.