Hey all,
I've been doing a *bunch* of researching on this topic, and I wanted to get
feedback from the community on this potential feature (or if I'm just
completely missing that it's already a feature, your gentle correction).
Recently, I've started a project, and I ran across a scenario where I had
to introduce duplicate scoping while defining an association. :(
Here's a summary of the scenario: There is one model that has one scope and
two associations to two other models. In one of the other models that the
first "joining" model is associated with, I have a has_many association
through this joined model. However, the complication (and duplication
arises) that the association has to be scoped to the scope on the joining
model. Currently (from what I've found through research), there's no way to
specify an *existing scope* on a :through association. The workaround is to
create a scope on the has_many association that does the exact same thing
that the :through association's scope already does.
An example will probably help illuminate the problem:
Assume we have the following models definitions:
class Book < ActiveRecord::Base
belongs_to :author, inverse_of: :books
belongs_to :publication, inverse_of: :books
scope :published, -> { where(is_published: true) }
# ...
end
class Author < ActiveRecord::Base
has_many :books, inverse_of: :author
has_many :publications, -> { where(books: { is_published: true }) },
through: :books
# ...
end
class Publication < ActiveRecord::Base
has_many :books, inverse_of: :publication
# ...
end
Notice the has_many :through association; this is currently how you'd have
to *manually scope* the "publications" association to achieve the same
effect of :
publications = books.published.map(&:publication)
I'd *like* to write something similar to:
has_many :publications, through: { :books => :published }
Which would indicate: "Using the :books association, apply the :published
scope, then retrieve all publications".
*Why this would be cool:*
1. Modularity
2. Less duplication
3. It would make at least *me* happy :D
Some Research Links:
Related (but outdated) SO question
<http://stackoverflow.com/questions/2783950/using-named-scopes-on-the-join-model-of-a-has-many-through>
Related (but also outdated) topic here on the forum
<https://groups.google.com/forum/?fromgroups#!searchin/rubyonrails-core/named_scope$20throuhg/rubyonrails-core/QeMbehZ7Aso/8AAABGNUWrEJ>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.