# How Do You Know What ActiveRecord Table Has a user\_id Attribute?

DevFeed: [How Do You Know What ActiveRecord Table Has a user\_id Attribute?](<https://devfeed.tech/articles/how-do-you-know-what-activerecord-table-has-a-user-id-attribute-28248.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2019/12/28/how-do-you-know-what-activerecord-table-has-a-user-id-attribute.html>)

Author: Fuzzygroup

Published: 2019-12-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Development](<https://devfeed.tech/topics/development.md>), [data-modeling](<https://devfeed.tech/topics/data-modeling.md>), [business logic](<https://devfeed.tech/topics/business-logic.md>)

Tags: [active-record](<https://devfeed.tech/tags/active-record.md>), [activerecord](<https://devfeed.tech/tags/activerecord.md>), [business-logic](<https://devfeed.tech/tags/business-logic.md>), [class](<https://devfeed.tech/tags/class.md>), [classes](<https://devfeed.tech/tags/classes.md>), [console](<https://devfeed.tech/tags/console.md>), [development](<https://devfeed.tech/tags/development.md>), [id](<https://devfeed.tech/tags/id.md>), [jumpstart](<https://devfeed.tech/tags/jumpstart.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [migration](<https://devfeed.tech/tags/migration.md>), [migrations](<https://devfeed.tech/tags/migrations.md>), [rails](<https://devfeed.tech/tags/rails.md>), [relationships](<https://devfeed.tech/tags/relationships.md>), [schema](<https://devfeed.tech/tags/schema.md>), [table](<https://devfeed.tech/tags/table.md>)

## AI overview

This article explains how to identify application classes whose ActiveRecord-backed tables have a user_id attribute. It introduces a DataObject class with a .has_user_id method that can be run in the console, helping the author add belongs_to user relationships without repeatedly checking the schema.

## Source excerpt

Even though I'm a firm, firm believer in agile, I've recently been experimenting with a throwback to waterfall style development and it very quickly left me with a fully featured data structure of tables and relationships modeled as example data. What I did is very rapidly write a series of migrations and then populate them to represent a sample "installation". My goal with this approach was to play to my strengths - data modeling - and avoid getting tied down in user interface stuff (my weakness). This was a very, very interesting approach and what I found was that I got much further along the lines of the "guts" of an application simply because I never got discouraged by: Oh Shite - I know this should look good but I'm too much of a hoser to make it look good; I guess I'll put it aside and go watch TV The downside to this is that I never bothered setting up the normal associations that you do when you write a migration; I simply thought about this application in terms of the example data: the users who would be using the system the data objects that they would create how the data objects would interact with each other One of the things that I noticed when I started filling in the basics like "belongs_to :user" was that I kept constantly jumping between the class I was working on and the schema file. Finally it hit me - what I needed was a method that I could execute in the console that would tell me what classes had a user_id attribute. And so I wrote a class called DataObject (for an ActiveRecord class which stores data) and a method .has_user_id. What I was looking for was output that looked like this: > DataObject.has_user_id Initiative Yes - has a user_id field KeyResultOwner Yes - has a user_id field KeyResult Yes - has a user_id field ObjectiveOwner Yes - has a user_id field ObjectiveType Objective Yes - has a user_id field OkrTeamMember Yes - has a user_id field OkrTeam Yes - has a user_id field OrganizationGroup Organization Quarter ResponsibilityRole Stat