Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Feature: has_many :through named_scope

#11

It would be nice to be able to use a has_many association through a named scope. E.g. if a user has many posts, and a post has many comments, one can use a named_scope in posts to define a scope for recent posts and a has_many association for users to get all comments of a user through his/her posts. However, you cannot get all comments on recent posts, because that'd require to do something like :through => 'posts.recent', which obviously doesn't work.

class Comment < ActiveRecord::Base
  belongs_to :post
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end

class User < ActiveRecord::Base
  has_many :posts

  # all comments of all posts of a user
  has_many :comments, :through => :posts

  # Doesn't work: all comments of all recent posts of a user
  #has_many :comments_on_recent_posts, :through => 'posts.recent', :source => :comments
end

Sure, one could use :conditions on the has_many association to scope the find, but that would mean that the advantage of the named_scope (defining the scope conditions in the model that it applies to) would be gone.

So, it would be nice if has_many :through could also work with a named scope, maybe by introducing a new option named :scope or :named_scope or :through_scope:

  has_many :comments_on_recent_posts, :through => :posts, :through_scope => :recent, :source => :comments

Reported by Andreas Neuhaus · April 16th, 2008 @ 06:09 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. josh
    josh
    • State changed from new to invalid

    I still think we are maintaining the policy of no "Feature Requests" without any sort implementation code.

    I'd try the mailing list to see if anyone has any ideas how to do it.

    April 16th, 2008 @ 07:38 PM

  2. Yong Bakos
    Yong Bakos
    • Tag set to activerecord, enhancement

    Per johnnyb on the old Trac ticket

    module ActiveRecord

    class Base

    def self.scoped_has_many_through(assoc, opts)

    has_many_scope = opts.delete(:scope)

    conditions_scope = Hash[*has_many_scope.to_a.map{|ent|

    ["#{opts[:through].to_s.pluralize}.#{ent[0]}", ent[1]]

    }.flatten]

    opts[:conditions] ||= {}

    if opts[:conditions].is_a?(Hash)

    opts[:conditions] = opts[:conditions].merge(conditions_scope)

    else

    raise "Conditions must be a hash for conditions_scope!"

    end

    has_many assoc, opts do

    #Using define_method instead of def in order to make it lexically scoped

    define_method(:construct_owner_attributes) do |reflection|

    atts = super(reflection)

    return atts.merge(has_many_scope)

    end

    end

    end

    end

    end

    scoped_has_many_through :reviewer_users, :through => :user_project_roles, :source => :user, :scope => { :role => "REVIEWER" }

    July 10th, 2008 @ 07:35 PM

  3. Chris Hapgood
    Chris Hapgood

    See this thread for recent discussion of this ticket as well as proposed solutions:

    http://groups.google.com/group/r...

    April 1st, 2009 @ 07:13 PM

  4. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:55 PM

  5. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 11th, 2010 @ 12:12 PM

  6. bingbing