<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harun Sabljaković</title>
    <description>The latest articles on DEV Community by Harun Sabljaković (@sabljak).</description>
    <link>https://dev.to/sabljak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F585114%2F1af6b0cc-f8f6-4e57-8d35-4b028a3d6f5c.jpeg</url>
      <title>DEV Community: Harun Sabljaković</title>
      <link>https://dev.to/sabljak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sabljak"/>
    <language>en</language>
    <item>
      <title>Dynamically direct routes for polymorphic associations - Ruby on Rails</title>
      <dc:creator>Harun Sabljaković</dc:creator>
      <pubDate>Tue, 12 Oct 2021 12:01:28 +0000</pubDate>
      <link>https://dev.to/wizardhealth/dynamically-direct-routes-for-polymorphic-associations-5hhb</link>
      <guid>https://dev.to/wizardhealth/dynamically-direct-routes-for-polymorphic-associations-5hhb</guid>
      <description>&lt;p&gt;Have you ever struggled with polymorphic associations? We all have, don’t worry.&lt;br&gt;
Here’s an example of a polymorphic relationship.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I’ve recently bumped into a problem where I had to list out all subjects (employees and products in this case) and when user interacted with one list item, it would redirect to that subject’s show page.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can see the problem, right? The obvious solution here would be to check the type of imageable and set the path helper accordingly. But, there’s a slightly more advanced technique for this use-case: direct method.&lt;br&gt;
In your routes.rb file simply add this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And now you can use imageable_path(imageable) as a path helper that will dynamically resolve to imageable’s show page.&lt;br&gt;
Now our example above becomes:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Aaaand voilà, we’ve got a path helper that resolves to any imageable show page without the need to manually check for the type of imageable.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
