Using rails and active record, is it possible to set the id to a self reference on create?
I am coming across an issue where a person is their own supervisor. I would like to set the supervisor_id field in the create statement for the person.
class Person < ActiveRecord::Base
belongs_to :supervisor, :class_name => "Person", :foreign_key => "supervisor_id"
...
Person.create(:name => "John Smith", :supervisor => ?self?)
From a mysql perspective, I don’t see how this is possible in a single query. Does rails have a built in way to do this? My current plan is a flag and an after create.
Since id’s aren’t assigned until after the object is created the cleanest way to do this in rails is with an after_create callback.
You might be able to do a mysql trigger if you want to get really advanced… but that seems like more trouble than its worth.