I’m trying to model a somewhat complicated genetic relationship in
Rails.
class Gene < ActiveRecord::Base
belongs_to :species, :foreign_key => “species_id”, :class_name =>
“Species”
has_many :genes_orthogroups, :class_name => ‘GenesOrthogroups’ #
join table model
has_many :orthogroups, :through => :genes_orthogroups
has_many :orthologs, :through => :orthogroups, :source
=> :genes, :conditions => ‘gene.species_id != ortholog.species_id’
end
class GenesOrthogroups < ActiveRecord::Base
belongs_to :gene
belongs_to :orthogroup
end
class Orthogroup < ActiveRecord::Base
has_many :genes_orthogroups, :class_name => ‘GenesOrthogroups’
has_many :genes, :through => :genes_orthogroups
belongs_to :species_pair, :class_name => ‘SpeciesPair’, :foreign_key
=> “species_pair_id”
end
class SpeciesPair < ActiveRecord::Base
belongs_to :species1, :foreign_key => “species1_id”, :class_name =>
‘Species’
belongs_to :species2, :foreign_key => “species2_id”, :class_name =>
‘Species’
has_many :orthogroups
end
class Species < ActiveRecord::Base
has_many :genes
has_many :species_pairs, :class_name => ‘SpeciesPair’
end
I’m trying to print out a list of the orthologs with a partial. The
list is generated with:
def find_ortholog_list(gene_id)
ortholog_list = []
Gene.find(gene_id).orthologs.each do |ortholog|
ortholog_list << ortholog
end
return OrthologList.new(ortholog_list)
end
I get the following error message:
Invalid source reflection macro :has_many :through for
has_many :orthologs, :through => :orthogroups. Use :source to specify
the source reflection.
If I change :source to :gene instead of :genes, I get:
Could not find the source association(s) :gene in model Orthogroup.
Try 'has_many :orthologs, :through => :orthogroups, :source =>
'. Is it one of :genes_orthogroups, :genes, or :species_pair?
Essentially, any given gene should have zero, one, or multiple
orthologs in another species. These are grouped into orthogroups, each
consisting of genes from two species, and representing a many-to-many
relationship. Genes in an orthogroup which are of the same species
would be paralogs instead. Because there are more than just two
species, a gene may participate in multiple orthogroups (up to one for
each additional species). The generalization for ortholog/paralog is
“homolog” or “homologue,” incidentally.
I’ve based most of this code on the p.369 explanation “Using Models as
Join Tables” in Agile Web D. with Rails (3rd Ed). I’m not
really sure why it fails, except for the fact that I’m trying to use
Gene as its own source for defining orthologs. What’s the proper way
to define this relationship?
Apologies for the complexity of the model. I couldn’t think of any
layman’s examples.
Thanks so much,
John