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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:14:52+00:00 2026-06-14T07:14:52+00:00

Assuming I am abstracting code, and am looping through the column names of object

  • 0

Assuming I am abstracting code, and am looping through the column names of object x, what’s the best way to detect if a column is an association or not?

I know I can do this, but I am wondering if there is a better way:

@user = User.first
  @user.attributes.keys.each do |column|
    if column[-3..-1] == "_id" && @user.respond_to?(column[0..-4].to_sym)
      puts "#{column} is an association / relation."
    else
      puts "#{column} is not an assocation / relation."
    end
  end
end

Any built-in Rails methods or helpers to detect associations? Code above is neither pretty nor fool proof. 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-06-14T07:14:53+00:00Added an answer on June 14, 2026 at 7:14 am

    One way to do this would be to reflect upon all associations for that class:

    associations = class_goes_here.reflect_on_all_associations
    

    And then to find just the belongs_to ones, since those will have the _id field:

    associations = associations.select { |a| a.macro == :belongs_to }
    

    Then you can find the foreign key used on these associations by doing this:

    association_foreign_keys = associations.map(&:foreign_key)
    

    I wouldn’t use @user.attributes to get the attributes and then keys on that to get the column names. I would use User.column_names to get the column names.

    Therefore, with all that explained, you can then change your code to be this to make it more foolproof:

    associations = User.reflect_on_all_associations
    associations = associations.select { |a| a.macro == :belongs_to }
    association_foreign_keys = associations.map(&:foreign_key)
    User.column_names.each do |column|
      if association_foreign_keys.include?(column)
        puts "#{column} is an association / relation."
      else
        puts "#{column} is not an assocation / relation."
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that parsing the equation would not be a problem, how can I make
Assuming I have a JQuery object stored in $e obtained through this selector: var
Assuming I can telnet to gitorious.org port 9418 (default for git), but not on
Assuming i have a post model with created_at, title, and id. How can i
Assuming class A { } class B : A { } covariance is not
Assuming that active directory is set up correctly, I'm trying to find a way
Assuming that N is very large, can anybody help me in ordering the following
Assuming Array was not overwritten, why the following comparisons output false? [] === new
Assuming a 10 element or less Object[] in Java, what would be the fastest
Assuming we are using an array implementation of a stack and not a linked-list

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.