Gordon Potter
Gordon Potter

Reputation: 5862

rails activerecord _id suffix in property

What is the implication of using a "_id" suffix for a field name in active record?

  t.string :foo_id

Does this always presume that it is a foreign key to "foo" table?

Is it better to use something like

  t.string :foo_id_value

to avoid ambiguity with rails?

The idea is that this id value has nothing to do with foreign keys and may not necessarily be unique.

Upvotes: 2

Views: 467

Answers (2)

Karl Sandwich
Karl Sandwich

Reputation: 11

You may wind up with a strange validation message.

Given a Rails 3 model with a validation like validates :foo_id, :presence => true, when you leave foo_id blank and validate, then you'll get the message "Foo can't be blank" instead of the expected "Foo id can't be blank".

Upvotes: 1

tom
tom

Reputation: 718

It's a matter of convention. In my experience you won't have any problems unless you do belongs_to :foo

If you want to avoid ambiguity for other developers, then yes, it's a good idea to avoid it.

Upvotes: 0

Related Questions