Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>because then your Favorite instance will not know what it is favouriting :) it knows that it <code>has_one :note</code>, but also has_one <code>:comment</code>, or? but not both surely.</p> <p><strong>polymorphic association the opposite way</strong> helps because it will express that a <code>Favorite</code> object belongs_to <code>:favorited</code> object that is polymorphic cos it can be any class the name of which will be stored in the <code>:favorited_type</code> string db column, so your favorite object will know that it favors a note or document or comment.</p> <p>with some code</p> <pre><code>class Note has_many :favorites, :as =&gt; :favorited has_many :fans, :through =&gt; :favorites, :source =&gt; :user end class Discussion has_many :favorites, :as =&gt; :favorited has_many :fans, :through =&gt; :favorites, :source =&gt; :user end class Comment has_many :favorites, :as =&gt; :favorited has_many :fans, :through =&gt; :favorites, :source =&gt; :user end class Favorite belongs_to :user belongs_to :favorited, :polymorphic =&gt; true # note direction of polymorphy end class User has_many :favorites has_many :favorite_notes, :through =&gt; :favorites, :source =&gt; favorited, :source_type =&gt; "Note" has_many :favorite_comments, :through =&gt; :favorites, :source =&gt; favorited, :source_type =&gt; "Comment" has_many :favorite_discussions, :through =&gt; :favorites, :source =&gt; favorited, :source_type =&gt; "Discussion" end </code></pre> <p>(just set up your db correctly) this design is standard for such usecase of favoriting.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload