Polymorphic Associations: dynamic finders

Is there any sort of dynamic finder for polymorphic associations?

For example if I had:

class InviteNode < ActiveRecord::Base
belongs_to :inviteable, :polymorphic => true
end

I would like to be able to search by:

InviteNode.find_by_inviteable(some_object)

instead of having to do:

InviteNode.find_by_inviteable_id_and_inviteable_type(some_object.id,
some_object.class.name)

obviously I could write my own, but am I missing something? Is there a
reason
Rails doesn’t provide something like this for you?

  • steve

steve dp wrote:

Is there any sort of dynamic finder for polymorphic associations?

For example if I had:

class InviteNode < ActiveRecord::Base
belongs_to :inviteable, :polymorphic => true
end

I would like to be able to search by:

InviteNode.find_by_inviteable(some_object)

instead of having to do:

InviteNode.find_by_inviteable_id_and_inviteable_type(some_object.id,
some_object.class.name)

obviously I could write my own, but am I missing something? Is there a
reason
Rails doesn’t provide something like this for you?

Because it’s a little tricky to code it up. I’ve got a test plugin for a
dynamic finder patch that does dynamic finders for belongs_to
associations already, and I’m working on the polymorphic version next.
But it’s not totally straightforward to do it in a way that makes sense.
I could bang out something that was gross, but I’m too proud to do that
:slight_smile:

http://blog.hasmanythrough.com/articles/2006/07/02/patch-test-plugin-dynamic-finders-with-belongs_to-names


Josh S.
http://blog.hasmanythrough.com