JapanRob
JapanRob

Reputation: 384

Rails, refer to an association by a custom, unique field

I have a Person object with the uuid column. Each person has_many blogs, each blog belongs_to a person.

I would like for blog.person_id to return the Person.UUID, and blog.person to return the person object. Is there a way to do this without making Person's primary key its own UUID?

Upvotes: 0

Views: 47

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

Mention the column name with primary_key option.

has_many blogs, primary_key: "uuid"

has_many options :

:primary_key

Specify the name of the column to use as the primary key for the association. By default this is id.

Upvotes: 3

Related Questions