Association extensions: how to access the object?


#1

With association extensions, like user.items.tagged(“foo”), how do I
access the “user” object from within the extension method? The code
would be e.g.

class User < ActiveRecord::Base
has_and_belongs_to_many :items do
def tagged(tag)
return “These are items tagged %s from user %s.” % [tag, ???]
end
end

but how would I get a hold of the user in question, within that method?

Since “self” in the method refers to the “items” collection, I can do
self[0].user, but that seems very much like a hack, and will obviously
not work if the items collection is empty.