Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marinados/4b2c0e559e95dea3c708 to your computer and use it in GitHub Desktop.
Save marinados/4b2c0e559e95dea3c708 to your computer and use it in GitHub Desktop.
gid = class_instance.to_global_id # method allowing to get a string with both model name and id
GlobalID::Locator.locate(gid) # method of getting the object by its global id (very useful for collections of multiple type of objects, e.g. for polymorphic associations)
Example :
In the model that has a polymorphic attribute detailable:
getter:
def global_detailable
detailable.to_global_id if detailable.present?
end
setter (for creation of a detailable as a nested attribute)
def global_detailable=(detailable)
self.detailable = GlobalID::Locator.locate(detailable)
end
view:
<%= f.grouped_collection_select :global_detailable, [@confirmed_sc_projects, @confirmed_projects], :all, :category_name, :to_global_id, :get_admin_details, include_blank: true %>
(grouped_collection_select allows to create groups of similar objects within a select)
array of different groups of possible select options
:all - method for select options (we're selecting all of them)
:category_name - optional name for groups of objects
:to_global_id - value method for the selected attribute
:get_admin_details - display method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment