Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Anonymous extension modules for all associations

#2186

has_one and belongs_to associations support the :extend option (although it isn't documented) but do not support anonymous modules as easily as has_many associations.

For the sake of completeness this patch allows anonymous modules for has_one, has_one :through and belongs_to associations, so you can do things like this:


belongs_to :author do
  def greeting
    "hello"
  end
end

has_one :last_comment, :class_name => 'Comment', :order => 'id desc' do
  def what_are_you
    "the last comment associated with a post..."
  end
end

It also adds the missing :extend option to the documentation.

Without the patch you are left to do something much uglier like this:


module MyBelongsToExtension
  def greeting
    "hello"
  end
end

class Post < ActiveRecord::Base
  belongs_to :author, :extend => MyBelongsToExtension
end

or this


class Post < ActiveRecord::Base
  belongs_to :author, :extend => ( Module.new do
    def greeting
      "hello"
    end
  end )
end

Neither of which are particularly nice :)

I should also give credit to Jonathan Viney for his original belongs_to patch from just over 2 years ago

Reported by Rob Anderton · March 9th, 2009 @ 11:10 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. tsailipu
    tsailipu

    +1 here.

    It took me quite some time to find out what is happening and track down why there is such inconsistency between belongs_to and, say, has_many, proxy extension design. It’s quite annoying (and quite abstraction- and documentation-defeating) that one has to look this deep into the source code to see what’s going on....

    Thanks for the patch!

    April 2nd, 2009 @ 02:00 PM

  2. Antonio Salazar Cardozo
    Antonio Salazar Cardozo

    +1

    Seems like unifying the API here is a Good Thing (tm).

    April 7th, 2009 @ 03:10 AM

  3. Michael Schuerig
    Michael Schuerig

    +1

    I just found this ticket while researching why the anonymous extension didn't work as I was expecting.

    May 25th, 2009 @ 01:34 AM

  4. Michael Schuerig
    Michael Schuerig

    I have a has_one :through association through an ordinary has_many association. If the has_one :through does not match any object, either because there aren't any or they don't match its condition, the extension method is not called on a proxy, but on nil.

    I haven't applied the patch yet, but I'd expect to see the same behavior for posts(:welcome).last_tag.what_are_you if there are no tags at all.

    May 25th, 2009 @ 02:32 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Claudio Poli
  7. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 29th, 2011 @ 01:00 AM

  8. rails
    rails
    • State changed from open to stale

    March 29th, 2011 @ 01:00 AM