I have the following models in my rails application
Model community
:
class Community < ActiveRecord::Base
has_many :community_program
end
Model community_program
:
class CommunityProgram < ActiveRecord::Base
belongs_to :program_name
end
Model program_name
:
class ProgramName < ActiveRecord::Base
has_many :community_programs
end
I am trying to find a way to pull all the program_names
which don't belong to a community_program
which has already been assigned to a community
.
I have been able to pull all program_names
and then remove the program_names
which are already associated with a community
but that is very expensive. Can I directly pull the second level unassociated records?
EDIT: The question is different from the linked question since in this case, program_name
might have an association with a Community Program for a different community but we still want to include that name if it doesn't have an association with the current community
.
**EDIT 2: I want to extract all the Program Names which haven’t yet been assigned to a Community Program associated with a particular Community. They can be associated with the Community Programs associated with a different community. The use case is that a user should be able to select a name from the list of remaining names and assign that to a community program associated with their community.
Program names are global and although the program name for community programs of a particular community are unique but different communities can have community programs associated to the same program name**