Conditional relation

Posted by Lowgain on Stack Overflow See other posts from Stack Overflow or by Lowgain
Published on 2010-05-18T00:09:54Z Indexed on 2010/05/18 4:10 UTC
Read the original article Hit count: 342

I've got a model like this:

Stem
  -id
  -etc

And then I have

Stemrelation
  -stem_id
  -related_stem_id
  -active

I can get the related stems with the following relations

class Stem < ActiveRecord::Base
  has_many :stemrelations
  has_many :related_stems, :through => :stemrelations
end

class Stemrelation < ActiveRecord::Base
  belongs_to :stem
  belongs_to :related_stem, :class_name => "Stem", :foreign_key => "related_stem_id"
end

But now I'd only like to get the active relations.

I tried adding this to the Stem model:

has_many :active_related, :through => :stemrelations, :source => :related_stem, :conditions => {:active => true}

but this gives me an error becasue it tries to check the active flag on the stem model instead of the stemrelation. What do I change here?

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about object-relational-mapping