Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection proxy loses its proxy_association when scoped further #2946

Merged
merged 5 commits into from
Jan 2, 2012
Merged

Collection proxy loses its proxy_association when scoped further #2946

merged 5 commits into from
Jan 2, 2012

Conversation

laserlemon
Copy link
Contributor

Say I have these GitHub-style models,

class User < ActiveRecord::Base
  has_many :projects do
    def personal
      where(:owner => proxy_association.owner.name)
    end

    def personal_first
      order("projects.owner <> '#{proxy_association.owner.name}'")
    end

    def visible_to(user)
      user == proxy_association.owner ? scoped : where(:private => false)
    end
  end
end

class Project < ActiveRecord::Base
end

Through the :projects association, I can get:

  • Only personal projects: @user.projects.personal
  • Projects ordered so the personal projects come first: @user.projects.personal_first
  • Projects that a user is allowed to see: @user.projects.visible_to(current_user)

The problem is that if I combine any two of these methods, it bombs, complaining about there being no proxy_association method defined on ActiveRecord::Relation.

I've updated the collection proxy to pass its @association on to further scopes by opening up a proxy_association accessor on ActiveRecord::Relation. I'd love to get your feedback. Thank you.

@joshpencheon
Copy link
Contributor

Seems sensible, +1.

@robmack
Copy link

robmack commented Dec 1, 2011

+1

@josevalim josevalim merged commit deaa682 into rails:master Jan 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants