No commit activity in last 3 years
No release in over 3 years
Define very basic, commonly used scopes for ActiveRecord associations. E.g. if a Post model has an author and a category association, scopes will be defined like Post.for(author) or Post.for(category).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 2.5.0
= 1.3.5

Runtime

>= 3.0.0
>= 0.8.7
 Project Readme

Scopes for Associations Build Status

https://github.com/ordinaryzelig/scopes_for_associations

Description

Define very basic, commonly used scopes for ActiveRecord associations.

Description through code

class Movie < ActiveRecord::Base
  belongs_to :director
  scopes_for_associations
end

Movie.for_director_id(Director.first.id)
#=> [#<Movie ..., director_id: 1>]
Movie.for_director(Director.first)
#=> [#<Movie ..., director_id: 1>]
Movie.for(Director.first)
#=> [#<Movie ..., director_id: 1>]

Supported association types

  • belongs_to (non-polymorpic and polymorphic)

If you're like me and prefer learning through looking at code, read through the specs. The examples here are basically reproductions of the tests.

belongs_to (non-polymorphic)

class Movie < ActiveRecord::Base
  belongs_to :director
  scopes_for_associations
end

Movie.for_director_id(Director.first.id)
#=> [#<Movie ..., director_id: 1>]
Movie.for_director(Director.first)
#=> [#<Movie ..., director_id: 1>]
Movie.for(Director.first)
#=> [#<Movie ..., director_id: 1>]

belongs_to (polymorphic)

class Movie < ActiveRecord::base
  # It is important to define this association.
  has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end

Comment.for_commentable_id(Movie.first.id)
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
Comment.for_commentable_type('Movie')
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>, ...]
Comment.for_commentable(movie)
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
Comment.for(Movie.first)
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]

Compatibility

Tested with ActiveRecord >=3.0.0