Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot favorite users using polymorphic association
    primarykey
    data
    text
    <p>Using the system below <strong>users</strong> are able to favorite <strong>styles</strong> in the format of: </p> <pre><code>=&gt; #&lt;Favorite id: 59, user_id: 2, favoritable_id: 15, favoritable_type: "Style", created_at: "2013-04-06 08:47:08", updated_at: "2013-04-06 08:47:08"&gt; </code></pre> <p>I have built the system to enable <strong>users</strong> to favorite <strong>users</strong> as well, but for some reason the line <code>Favorite.create!(:user =&gt; user)</code> is not creating the expected favorite model object when called on <strong>User</strong>, as above when called on <strong>Style</strong>. Instead I am getting nils as below:</p> <pre><code>=&gt; #&lt;Favorite id: 65, user_id: 2, favoritable_id: nil, favoritable_type: nil, created_at: "2013-04-06 09:35:07", updated_at: "2013-04-06 09:35:07"&gt; </code></pre> <hr> <hr> <p><strong>Problem is above. Details are below</strong></p> <hr> <hr> <p>Favorite Class:</p> <pre><code>class Favorite &lt; ActiveRecord::Base belongs_to :favoritable, :polymorphic =&gt; true belongs_to :user attr_accessible :user validates_presence_of :user validates_uniqueness_of :user_id, :scope =&gt; [:favoritable_type, :favoritable_id] end </code></pre> <p>Model definitions including favoritable module (see below):</p> <pre><code>class User &lt; ActiveRecord::Base include Favoritable class Style &lt; ActiveRecord::Base include Favoritable </code></pre> <p>Favoritable Module:</p> <pre><code>module Favoritable extend ActiveSupport::Concern included do attr_accessible :favorites has_many :favorites, :as =&gt; :favoritable, :autosave =&gt; true accepts_nested_attributes_for :favorites def add_favorite(user) self.favorites &lt;&lt; Favorite.create!(:user =&gt; user) save end def delete_favorite(user) self.favorites.where(:user_id =&gt; user.id).each do |favorite| favorite.destroy end end end end </code></pre> <p>Favorites Controller:</p> <pre><code>class FavoritesController &lt; ApplicationController def fav_user @user = User.find(params[:id]) @user.add_favorite(current_user) respond_to do |format| format.js { render :action =&gt; "update_favorite_disp", :layout =&gt; false } end end def delete_fav_user @user = User.find(params[:id]) respond_to do |format| if @user.delete_favorite(current_user) format.js { render :action =&gt; "update_favorite_disp", :layout =&gt; false } end end end def fav_style @style = Style.find(params[:id]) @style.add_favorite(current_user) respond_to do |format| format.js { render :action =&gt; "update_favorite", :layout =&gt; false } end end def delete_fav_style @style = Style.find(params[:id]) respond_to do |format| if @style.delete_favorite(current_user) format.js { render :action =&gt; "update_favorite", :layout =&gt; false } end end end end </code></pre>
    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.
 

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