en en en en id en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en sk es en en en en en en en en fr en en en en en fr en en en en en en en en en en en en en en en en en fi en en en en de en en en en en en en en en en en en en en en en en en en hbs_lat en en en hbs_lat en en en en en en en en en en en en en en en en en en en en en en tr en en en en en en en en en en en en en en en en en en en en en en en en en en pt en en en en en en en en en en en en en en
As I stumble through the world of linux, I've decided to log new tricks I learn because I often forget. I decided to share the log because…. eh, why not.
Formatting Numerical Input with Rails
Kubuntu 16.04 GUI Audio Controls not working
Polymorphic Associations The Smart Way: Using Global Ids
Get Firefox to display warning message when exiting multiple tabs
jaehoo on Kubuntu 16.04 GUI Audio Contro…
BennyBeat on How to download a file from so…
fastrapidleech.com on How to download a file from so…
neanderslob on How to download a file from so…
Polymorphic Associations The Smart Way: Using Global Ids
November 3, 2015December 5, 2015 neanderslobglobal id, polymorphic associations, rails, ruby on railsLeave a comment
Global IDs have previously been a feature of rails with the help of a gem but as of Rails 4.2, it’s officially baked into the platform.  I just recently used the new addition to create polymorphic associations within an app and I have to say, it’s fantastic.  I did, however, find very little help out there for first-time users of this feature so here’s a few notes on how to do it.
What is a global id
Global Id is a library that produce Uniform Resource Identifiers (URIs) for any piece of data you’d like. They tend to look something like this:
Global IDs have previously been a feature of rails with the help of a gem but as of Rails 4.2, it’s officially baked into the platform.  I just recently used the new addition to create polymorphic associations within an app and I have to say, it’s fantastic.  I did, however, find very little help out there for first-time users of this feature so here’s a few notes on how to do it.
What is a global id
Global Id is a library that produce Uniform Resource Identifiers (URIs) for any activerecord object. They tend to look something like this:
So global IDs allow all of this information to be expressed in a single string very quickly.  With a couple methods in the model we can use this to create a polymorphic association.
Cool, so how do I make this happen?
Let’s pretend that we want to set up an association to indicate whether a Stockholder is a Person or a Company.
First, we need to generate our stockholder model
We will need two columns: entity_id and entity_type. Just like in the standard polymorphic association, the latter stores the model name, the former stores the id within that model.  We can generate both with a single line in our migration as follows:
class CreateStocholders < ActiveRecord::Migration
create_table :optionees do |t|
t.belongs_to :option, index: true, foreign_key: true
t.references :entity, polymorphic: true, index: true
Next, we set up our models to indicate a polymorphic relationship
class Company < ActiveRecord::Base
has_many :stockholders, as: :entity
class Person < ActiveRecord::Base
has_many :stockholders, as: :entity
class Stocholder < ActiveRecord::Base
In the above case, :entity is just catch-all field that represents people and companies. Of course, you can call yours whatever you’d like.
Now we can set up our form for the stockholder
I use simple_form but the principle is the same for other methods; adjust your syntax accordingly. Something like this.
So obviously, the exciting part here is the grouped_collection_select which will render a nice select menu with both companies AND people in it.
To break it down:
This is a grouped collection for a client
Over the models Company and Person
It will use all of the records in each model
and group them according to their model_name.
It will use the global_id to get the values
and display the email of each in the menu
The above will give you a grouped selector similar to the one shown below (note I used Org instead of Company, but you get the idea).
But you might rightly ask: “Why did you use email addresses and not names?”  The tricky thing about these sorts of associations is that people have both a first name and a last name where companies just have a name.  In order to get names to render in such a situation, you will need to use a lambda, which I’ll go over at the end of the post.
Making Global Ids work with Polymorphic Associations
If you’ve been keeping track though you should see that we have created two key problems for integrating the global id with polymorphic associations:
We need :entity_type as well as :entity_id
:entity_id needs to be an integer, not a string as is a global id.
So, to rephrase the situation, we need to break the global id into two pieces:
Its model name (a string)
Its id for that model (an integer)
Fortunately we don’t need to do this manually, rails polymorphic associations will store the model name and id automatically if it knows which object to reference.  Therefore, we specify getter/setter methods to specify the object, using global ids.
So, editing the Stockholder model again:
class Stocholder < ActiveRecord::Base
Then change the grouped_collection_select to:
Then just be sure to add :global_entity to your strong params and that’s it!
Using the lambda for names
Ok, often when doing polymorphic associations, one will use different pieces of information as keys for the two different models (after all, they’re not always going to have the same columns, right?)  In such a case, we use a lambda, making our select like this:
As an explanation of lambdas is a bit beyond the scope of this tutorial, I’ll just leave you with the example above.  There is plenty of documentation available out there on the matter so if the lambda’s confusing, just google it up.
Anyway, that’s it; you’re done!
Polymorphic Associations The Smart Way: Using Global Ids
Create a free website or blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.