# "for column in..." not picking up \_id columns

**URL:** <https://www.ruby-forum.com/t/for-column-in-not-picking-up-id-columns/94294>\
**Category:** Rails\
**Created:** [April 27, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/for-column-in-not-picking-up-id-columns/94294 "2006-04-27T23:37:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [April 27, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/for-column-in-not-picking-up-id-columns/94294/1 "2006-04-27T23:37:16Z")

</div>

My code executes fully:

\<% for column in ModelName.content\_columns %\>

\<%= column.human\_name %\>  
\<% end %\>

but it does not pick up any columns in my model’s table that end in  
“\_id”.

Is this by design?

Is there a helper method that will reference the related tables? Or  
will I need to code the column titles and detail records by hand?

Learning…

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [April 27, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/for-column-in-not-picking-up-id-columns/94294/2 "2006-04-27T23:54:21Z")

</div>

Ryan wrote:

> 

Yes. From  
[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000888](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000888)

“Returns an array of column objects where the primary id, all columns  
ending in “\_id” or “\_count”, and columns used for single table  
inheritance have been removed.”

> Is there a helper method that will reference the related tables? Or  
> will I need to code the column titles and detail records by hand?

You want to use ModelName.columns if you want the columns ending in \_id.

> Learning…

[http://api.rubyonrails.com/](http://api.rubyonrails.com/) is your friend and teacher. You can look at  
the source for content\_columns if you want to reject some of the other  
columns that content\_columns rejects, but keep the columns ending in  
\_id.

–

Ray

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [April 27, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/for-column-in-not-picking-up-id-columns/94294/3 "2006-04-27T23:56:45Z")

</div>

Ryan wrote:

> My code executes fully:
> 
> \<% for column in ModelName.content\_columns %\>
> 
> \<%= column.human\_name %\>  
> \<% end %\>
> 
> but it does not pick up any columns in my model’s table that end in  
> “\_id”.
> 
> Is this by design?
> 
> Is there a helper method that will reference the related tables? Or  
> will I need to code the column titles and detail records by hand?
> 
> Learning…

The …\_id columns are not “content\_columns” according to rails.  
They’re used to link to other models.

If you have Company containing many People, the people table will have a  
company\_id that links back to the company.

In Rails, you’d have the following models:

class Person \< ActiveRecord::Base  
belongs\_to :company  
end

class Company \< ActiveRecord::Base  
has\_many :people  
end

With that in place, and the correctly named \_id field in the people  
table, you can do the following:

person = Person.find(:first)  
person.company (this is a Company object)

or

company = Company.find(:first)  
company.people (this is an array of Person objects)

You could add person to the company like this:

company.people \<\< person

I recommend you stop typing at this point and read the Agile Web Dev  
book before going any further. I promise it’ll be worth it.

Alan
