So the plugin appears to work. FYI, it's here:
git://github.com/ianwhite/nested_has_many_through.git

Unfortunately, I'm still having trouble setting up the condition. I've
renamed a few things and added appropriate associations:

class Gene < ActiveRecord::Base
  belongs_to :species
  has_many :gene_orthogroup_linkers, :class_name =>
'GeneOrthogroupLinker'
  has_many :orthogroups, :through => :gene_orthogroup_linkers
  has_many :ortholog_orthogroup_linkers, :class_name =>
'GeneOrthogroupLinker', :through => :orthogroups, :source
=> :gene_orthogroup_linkers
  has_many :orthologs, :source => :gene, :through
=> :ortholog_orthogroup_linkers, :conditions => 'genes.species_id !=
genes_2.species_id'
end

GeneOrthogroupLinker is the class that used to be GenesOrthogroups.

Mysql::Error: Unknown column 'genes_2.species_id' in 'where clause':
SELECT `genes`.* FROM `genes`   INNER JOIN gene_orthogroup_linkers ON
( genes.id = gene_orthogroup_linkers.gene_id )  INNER JOIN orthogroups
ON ( gene_orthogroup_linkers.orthogroup_id = orthogroups.id )  INNER
JOIN gene_orthogroup_linkers gene_orthogroup_linkers_2 ON
( orthogroups.id = gene_orthogroup_linkers_2.orthogroup_id )
WHERE (gene_orthogroup_linkers_2.gene_id = 556  AND genes.species_id !
= genes_2.species_id)

So, it looks to me like the problem is that it selects from genes but
has no inner join to genes_2, only to gene_orthogroup_linkers_2. How
do I get it to INNER JOIN genes genes_2 ON
( gene_orthogroup_linkers_2.gene_id = genes_2.id AND genes.species_id !
= genes_2.species_id)?

It seems like as a work-around, I could add a species_id column on the
GeneOrthogroupLinker model, but that doesn't seem like the cleanest
solution.

Incidentally, it seems unnecessary to join with orthogroups (why not
just join the two linkers with orthogroup_id?). Is this easily
fixable, or better left as-is?

I also tried the named_scope, but it doesn't seem right for this
situation.

Best,
John


On Jun 29, 10:46 am, Matt Jones <al2o...@gmail.com> wrote:
> The :orthologs association is the problem - Rails doesn't support
> nesting :through associations. I recall there being a plugin around
> someplace to do it, so you may want to look into that.
>
> Depending on what you need, a simple instance method may work as well.
> For example (on Gene):
>
> def orthologs
>   genes_orthogroups.ortholog_groups_for(self).map { |g| g.gene }
> end
>
> On GenesOrthogroup:
> named_scope ortholog_groups_for { |g| { :include => :gene, :conditions
> => ['genes.species_id != ?', g.species_id] } }
>
> (not tested, but should be close to working)
>
> A couple general things:
>
> - model names should be singular (GenesOrthogroup rather than
> GenesOrthogroups). Otherwise you'll eventually run into issues.
>
> - when writing SQL fragments in conditions, table names are plural (so
> genes.whatever rather than gene.whatever).
>
> - the association macros have sensible defaults, so you can leave some
> options out. For instance, the :species association in Gene can be
> simplified to 'belongs_to :species' - Rails will find the correct FK
> (species_id) and class (Species).
>
> --Matt Jones
>
> On Jun 28, 8:07 pm,JohnWoods<john.wo...@marcottelab.org> wrote:
>
> >   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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to