andy
andy

Reputation: 2399

Rails rename the ID column in a model

I'm currently looking at migrating an existing system (written in spaghetti PHP) over to rails. The problem is, it has to run off of a live database. A lot of the ID columns on these different tables aren't named id. For instance, the customers table has an ID column called Customer_ID. Upon looking, I just realised that rails does infact seem to find by the primary key instead of a specific column called id.

Will I face a lot of problems later with the naming of these ID columns, specifically in stuff like relationships?

Upvotes: 3

Views: 2693

Answers (2)

Joonsoo Kang
Joonsoo Kang

Reputation: 76

After v2.3.8, set_primary_key :col_name is deprecated.

self.primary_key = 'col_name' is recommended.

http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods.html

Upvotes: 6

Sachin R
Sachin R

Reputation: 11876

Change primary key attribute in model by using

set_primary_key :col_name

Upvotes: 2

Related Questions