<?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: Zahid Ali</title>
    <description>The latest articles on DEV Community by Zahid Ali (@zahidali).</description>
    <link>https://dev.to/zahidali</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%2F691212%2F4d3b0a60-6dbc-403a-9c90-c4840a25ae60.jpeg</url>
      <title>DEV Community: Zahid Ali</title>
      <link>https://dev.to/zahidali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zahidali"/>
    <language>en</language>
    <item>
      <title>Polymorphic Associations in Ruby on Rails</title>
      <dc:creator>Zahid Ali</dc:creator>
      <pubDate>Sun, 11 Dec 2022 21:17:06 +0000</pubDate>
      <link>https://dev.to/zahidali/polymorphic-associations-in-ruby-on-rails-31pd</link>
      <guid>https://dev.to/zahidali/polymorphic-associations-in-ruby-on-rails-31pd</guid>
      <description>&lt;p&gt;In Ruby on Rails, a polymorphic association is a type of association that allows a model to belong to more than one other model on a single association. This is useful when you have a model that can be associated with multiple other models in a single association.&lt;/p&gt;

&lt;p&gt;Here is an example of using a polymorphic association to define a relationship between a &lt;code&gt;Comment&lt;/code&gt; model and both a &lt;code&gt;Post&lt;/code&gt; and a &lt;code&gt;Video&lt;/code&gt; model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Comment &amp;lt; ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

class Post &amp;lt; ApplicationRecord
  has_many :comments, as: :commentable
end

class Video &amp;lt; ApplicationRecord
  has_many :comments, as: :commentable
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;Comment&lt;/code&gt; model &lt;code&gt;belongs_to&lt;/code&gt; a &lt;code&gt;commentable&lt;/code&gt; model using a polymorphic association. This means that a &lt;code&gt;Comment&lt;/code&gt; can belong to either a &lt;code&gt;Post&lt;/code&gt; or a &lt;code&gt;Video&lt;/code&gt; model, depending on the value of the &lt;code&gt;commentable_type&lt;/code&gt; attribute. The &lt;code&gt;Post&lt;/code&gt; and &lt;code&gt;Video&lt;/code&gt; models both &lt;code&gt;has_many&lt;/code&gt; &lt;code&gt;Comment&lt;/code&gt; objects, specifying that they are the &lt;code&gt;commentable&lt;/code&gt; type for those &lt;code&gt;Comment&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;Once the associations are defined, you can use them to access the associated objects in your code. For example, you can use the &lt;code&gt;post.comments&lt;/code&gt; method to access the &lt;code&gt;Comment&lt;/code&gt; objects associated with a &lt;code&gt;Post&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post = Post.find(1)
post.comments    # =&amp;gt; Returns an array of `Comment` objects associated with the `Post`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the &lt;code&gt;video.comments&lt;/code&gt; method to access the &lt;code&gt;Comment&lt;/code&gt; objects associated with a &lt;code&gt;Video&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;video = Video.find(1)
video.comments    # =&amp;gt; Returns an array of `Comment` objects associated with the `Video`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Overall, polymorphic associations are a useful way to define relationships between models in Ruby on Rails when a model can belong to multiple other models on a single association. By using polymorphic associations, you can easily access and manipulate the associated objects in your code.&lt;/p&gt;

</description>
      <category>polymorphic</category>
      <category>rails</category>
      <category>associations</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
