You can solve your problem by adding another association:
class Project < ActiveRecord::Base
has_one :leader, -> { where(is_lead: true) }, class_name: 'Person'
validates :leader, presence: true
end
When you create a Project
you can set a lead pretty easily:
def create
project = Project.new(params[:project])
project.leader.new(name: 'Corey') #=> uses the scope to set `is_lead` to `true`
end
You still have the lead
scope duplicated in your Person
model, but since that's already defined, let's just use it:
class Project < ActiveRecord::Base
has_one :leader, Person.method(:lead), class_name: 'Person'
end
This has the upside of making it a lot easier to grab the leader of a project, too.