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 943939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:30:50+00:00 2026-05-15T22:30:50+00:00

I want ActiveRecord to lookup by a non-id column from a table. Hope this

  • 0

I want ActiveRecord to lookup by a non-id column from a table.
Hope this is clear when I give you my code sample.

class CoachClass < ActiveRecord::Base
  belongs_to :coach
end

class Coach < ActiveRecord::Base
    has_many :coach_classes, :foreign_key => 'user_name'
end

When I do a
coach_obj.coach_classes, this rightly triggers

SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2)

(2 being the that coach’s id here which is my problem.)

I want it to trigger

SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David')

(‘David’ being the that coach’s user_name)

user_name is unique and present in both tables.

I do not want to have a coach_id in my coach_classes table for some reason.

  • 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-15T22:30:51+00:00Added an answer on May 15, 2026 at 10:30 pm

    I think you need to specify the primary key options on the associations as well:

    class CoachClass < ActiveRecord::Base 
      belongs_to :coach, :foreign_key => 'user_name', :primary_key => 'user_name'
    end
    
    class Coach < ActiveRecord::Base
      has_many :coach_classes, :foreign_key => 'user_name', :primary_key => 'user_name'
    end 
    

    This specifies the method that returns the primary key of the associated object (defaulting to id).

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

Sidebar

Related Questions

I want to swap the content in answers table with ActiveRecord. code 1: Archieve::Answer.find_each
I want to rename a table... (any table.) I tried this line of code:
I want to change the table name of the ActiveRecord model class during request
I want to have a delete user link in a normal Activerecord table, but
I have a simple class like this [ActiveRecord(Subscriptions)] public class Subscription : ActiveRecordBase<Subscription> {
In Ruby on Rails, where does one put the code from this snippet in
I want to include this module in every ActiveRecord model in my Rails app,
I want to create a model 'Relation' which extends ActiveRecord::Base, set it's table name
class Post< ActiveRecord::Base end post_array = Post.first If I want to add some data
I want my ActiveRecord class User to contain options (a bunch of string key-values),

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.