3.2rc1 through association with dynamic conditions broken when using joins/includes

18 views
Skip to first unread message

Victor Lin

unread,
Dec 24, 2011, 12:00:18 AM12/24/11
to rubyonra...@googlegroups.com
My app has feeds and items. Users can subscribe to feeds, which I track in a Subscription class.

I wanted an association for all of a user's unread items:

class Subscription < ActiveRecord::Base
  belongs_to :feed
  belongs_to :user
  has_many :unread_items, :through => :feed, :source => :items, :conditions => proc {
    "not exists ( select * from readings where readings.item_id = items.id AND readings.user_id = #{user_id} )"
  }

...

This works fine if I reference it directly (i.e, @user.subscriptions.first.unread_items). But if I try to eager load or join against it:

eager load: 'self' in the conditions proc refers to the intermediate object (here, the Feed class) which I don't think is expected. Crashes because Feed obviously has no user_id in my app.
join:  'self' in the conditions proc refers to a JoinDependency object, which seems totally wrong. 

Victor Lin

unread,
Dec 24, 2011, 12:01:42 AM12/24/11
to rubyonra...@googlegroups.com
Er, to clarify, the error I'm getting is that the interpolation of #{user_id} in the association condition is undefined. 
Reply all
Reply to author
Forward
0 new messages