Skip to content

Instantly share code, notes, and snippets.

@mettledrum
Created May 29, 2014 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mettledrum/db2faa1a94ff4302f54e to your computer and use it in GitHub Desktop.
Save mettledrum/db2faa1a94ff4302f54e to your computer and use it in GitHub Desktop.
# mixes in logic to make a model understand how to fetch its relationships
# based on the item_id/item_item_type field combination
module VariadicItem
extend ActiveSupport::Concern
included do
belongs_to :item_type
end
# get the item associated with the current polymorphic record into which this module has been mixed
def item
item_type.name.classify.constantize.find(item_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment