This project is archived and is in readonly mode.

#11 ✓invalid
Andreas Neuhaus

Feature: has_many :through named_scope

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

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

Comments and changes to this ticket

  • josh

    josh April 16th, 2008 @ 07:38 PM

    • 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.

  • Yong Bakos

    Yong Bakos July 10th, 2008 @ 07:35 PM

    • 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" }

  • Chris Hapgood

    Chris Hapgood April 1st, 2009 @ 07:13 PM

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

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

  • Ryan Bigg

    Ryan Bigg October 9th, 2010 @ 09:55 PM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 12:12 PM

    Automatic cleanup of spam.

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>