March 16

0 comments

Don’t end the name of fields in Ruby with ‘_id’ unless you are doing an association

By Christopher Mendla

March 16, 2016


Last Updated on September 15, 2020 by Christopher G Mendla

I was naming a field for a Ruby/Rails project. Since the field referred to an id for something, I named it according.. For example, product_id. That seemed to make sense. 

However, I use a sorting routine to make the column names sortable on a click. 

When I ran it through the sortable routine to make the columns sortable, I got a crash.

Apparently naming a field with an field name ending in _id gets things confused with associated fields. 

I’m not sure if it affects anything outside of the sortable routine but my guess is that it could so I’d rather play it safe. 

The problem arises because associated tables/fields in Rails depend on a key field with a field name ending in _id. 

Ruby 2.x/Rails 4.x

Christopher Mendla

About the author

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}