I am doing a coding project in rails. I have a user model, and I want to add a "spouse_id" to it (which would reference other users). Can a model reference itself in a foreign key?
- What is the data store, for those of us not familiar with Rails? In some data stores, joins of any kind are problematic, and Rails is not the issue.Robert Harvey– Robert Harvey2015-07-24 14:39:44 +00:00Commented Jul 24, 2015 at 14:39
- I'm not sure what you mean, but I am storing the data in a database (sqlite3 in dev; postgres in prod)Jeff– Jeff2015-07-24 14:40:21 +00:00Commented Jul 24, 2015 at 14:40
- 2Then the answer is "of course." It's called a self-join.Robert Harvey– Robert Harvey2015-07-24 14:40:51 +00:00Commented Jul 24, 2015 at 14:40
- 1Thanks @RobertHarvey, I'm still new at this, never heard of a self-join. Now that I know what to look for: railscasts.com/episodes/163-self-referential-associationJeff– Jeff2015-07-24 14:41:26 +00:00Commented Jul 24, 2015 at 14:41
- 2Also called a Self-referencing table.Robert Harvey– Robert Harvey2015-07-24 14:42:22 +00:00Commented Jul 24, 2015 at 14:42
| Show 1 more comment