Forum: Ruby on Rails Association extensions: how to access the object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Henrik N (Guest)
on 2006-04-06 14:16
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.
This topic is locked and can not be replied to.