Reputation: 9491
assume you have a case like this
class Artist < ActiveRecord::Base
has_many :albums
belongs_to :record_label
def albums
return 5
end
end
Is it possible to access the albums relationship without using the Artist#albums, since it has been overridden?
This can happen with mixins or other random cases, it is mostly helpful for tests. Then you can say Artist#albums is actually a relationship to albums
Upvotes: 0
Views: 56
Reputation: 96914
Use association
, and retrieve its scope:
a = Artist.first
a.association(:albums).scoped
Note that association
is not documented, and neither is the object it returns (ActiveRecord::Associations::Association
), which means scoped
isn't documented either.
Upvotes: 1
Reputation: 3040
May I ask - why do you need to override #albums
? This has the disadvantage of violating the Principle of Least Surprise - everybody would expect #albums
just to return the artist's albums.
Otherwise, I would access them simply by Album.where(artist_id: artist.id)
.
Upvotes: 0