how do you define association conditions that reference the parent table to work for target find and join association #5555

Closed
pfenig opened this Issue Mar 23, 2012 · 1 comment

Projects

None yet

2 participants

@pfenig
Contributor
pfenig commented Mar 23, 2012

I'm on 3.0. But I noticed in the new 3.2 documentation it actually talks about how the proc gets evaluated in the different contexts.

The best I have come up with is something like this:

has_many :things,
  :conditions => proc { 
    case self
    when ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
      "#{aliased_table_name}.shared_id = #{parent_table_name}.shared_id"
    else
      {shared_id: shared_id} 
    end
  }

This allows both the following to work:

 some_object.things
 #and
 SomeClass.joins(:things)
@drogus
Member
drogus commented Mar 23, 2012

Please do not post questions to issues tracker, it's for bugs and there is already too much things in here.

@drogus drogus closed this Mar 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment