This project is archived and is in readonly mode.

#2186 ✓stale
Rob Anderton

Anonymous extension modules for all associations

Reported by Rob Anderton | March 9th, 2009 @ 11:10 PM | in 3.x

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

Comments and changes to this ticket

  • tsailipu

    tsailipu April 2nd, 2009 @ 02:00 PM

    +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!

  • Antonio Salazar Cardozo

    Antonio Salazar Cardozo April 7th, 2009 @ 03:10 AM

    +1

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

  • Michael Schuerig

    Michael Schuerig May 25th, 2009 @ 01:34 AM

    +1

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

  • Michael Schuerig

    Michael Schuerig May 25th, 2009 @ 02:32 AM

    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.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Claudio Poli
  • rails

    rails March 29th, 2011 @ 01:00 AM

    • 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.

  • rails

    rails March 29th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages