Skip to content

Instantly share code, notes, and snippets.

@jameslafa
Created December 9, 2015 16:09
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 jameslafa/a1913c5d13f42ca87168 to your computer and use it in GitHub Desktop.
Save jameslafa/a1913c5d13f42ca87168 to your computer and use it in GitHub Desktop.
Transfert enumerator accessors to related model
class User < ActiveRecord::Base
belongs_to :organization
Organization.categories.keys.each do |category|
define_method("#{category}?") do
organization.send("#{category}?")
end
end
end
# Organization model has an enum category with following values: admin, insurance, practice
# It generated automatically on the organization model admin?, insurance?, practice?
# To have it also on the user model (belonging to Organization), we can create dynamically this method with define_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment