Our Rails 3 application has models Person and Message. Messages can be specific to a Person (when the message person_id column is set) or they can be "global" (when the person_id column is NULL).
We would like to have a simple has_many relationship using the :conditions option as such:
class Person < ActiveRecord::Base
has_many :messages,
:conditions => proc { ['(messages.person_id IS NULL) OR ' +
'(messages.person_id = ?)'], self.id }
# ...
end
But it appears that the has_many class method encodes the "conditions" option as a logical "AND" clause after enforcing the foreign key constraint of equality to the Person object's id (e.g. "FROM messages WHERE person_id=123 AND (person_id IS NULL OR person_id=123)"). It appears that there is no way to allow associated objects with null foreign keys to belong to such associations.
Does Rails 3 / ActiveRecord provide a way to do this or must I hack my own association-like methods?