#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

  • Joshua Peek

    Joshua Peek 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 changed from “” 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" }

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins