Tag Archives: plpgsql

Exclusive favorite association for ActiveRecord has_many

It’s not uncommon in my application to maintain an exclusively preferred selection out of a collection on an object. For example:

class Person < ActiveRecord::Base
has_many :employers, :class_name => ‘Employee’
end
 
class Company < ActiveRecord::Base
has_many :employees
end
 
class Employee < ActiveRecord::Base
belongs_to :person
belongs_to :company
 
named_scope :favorite, {:conditions => {:favorite => true}}
[...]