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

  • SEARCH
  • Home
  • 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 6950365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:05:00+00:00 2026-05-27T14:05:00+00:00

I have 2 models in my rails app, one with an UUID primary key

  • 0

I have 2 models in my rails app, one with an UUID primary key :

class User < ActiveRecord::Base
  belongs_to :country, :foreign_key => 'country_uuid'
end

class Country < ActiveRecord::Base
  set_primary_key :uuid
  has_many :users
end

When I try something like that:

<% @user = User.find :first, :include => [:country] %>
<%= @user.country.name %>

I have the good result, but I see 2 requests in the log file. Why eager loading is not working when we change the ID key for UUID key ?

User Load (0.4ms)  SELECT `users`.* FROM `users` LIMIT 1
Country Load (0.4ms)  SELECT `countries`.* FROM `countries` WHERE (`countries`.`uuid` = '1')

And I would have something like:

User Load (0.4ms)  SELECT `users`.* FROM `users` INNER JOIN countries ON countries.uuid = users.country_uuid LIMIT 1

Is there a workaround ?
If I change uuid key for id key, but keep the string format to store an uuid, will it be ok ?

Thanks,

  • 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-05-27T14:05:01+00:00Added an answer on May 27, 2026 at 2:05 pm

    Use joins instead of include to get the inner join

    includes always issues a 2nd query but not n+1 queries (lazy)

    for the direction you are going in user -> 1 country it is not so important

    but if you were going the other direction country -> many users

    country = Country.first
    # => select countries.* from countries where id = xxxx limit 1;
    country.users.each do 
        # select users.* from users where user_id = xxxx;
        # this could be bad because of lazy loading, one query per iteration
    end
    
    # vs...
    country = Country.first.includes(:users)
    # => select countries.* from countries where id = xxxx limit 1;
    # => select users.* from users where country_uuid IN (xxxx);
    country.users.each do
        # users are all in memory
    end
    

    see http://guides.rubyonrails.org/active_record_querying.html for more info

    I don’t think the fact you are using UUID should make any difference

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

Sidebar

Related Questions

I have a rails 3.1 app with the following Models: class Post < ActiveRecord::Base
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I have a Rails app, with 2 models : a User model and a
In my Rails 2.3.2 app I have 2 models: class Post has_many :approved_comments, :class_name
I have a ruby (1.9.3) on rails (3.1) app where: app/models/list.rb class List <
I have a rails app that has two models one is vender and the
I have a rails app which currently has 3 models: Post , User and
I have a simple Rails 3 app: no models, one view and one controller
I have a Rails app with the following (simplified) models: # member.rb class Member
I have a couple of models in a Rails app. Let's say it's Products

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.