Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's start with some style and design issues and end with the actual answer:</p> <ol> <li><p>Models are singular by convention. Doing otherwise will only cause you more work. In this case, I would suggest <code>Following</code> as a suitable name, as in "a user has many followings".</p></li> <li><p>Foreign keys should end with <code>_id</code>. Doing otherwise will only cause you more work. So <code>follower_id</code> and <code>followed_id</code>.</p></li> <li><p>Methods that are intended to be used for their true/false nature ("<a href="http://reinh.com/blog/2008/07/16/ruby-patterns-query-method.html">query methods</a>") should end with a ?, so <code>follows?</code> instead of <code>follows</code>,</p></li> <li><p>Your if statement is redundant and can be safely removed once the condition does the right thing. In ruby, in the context of conditionals, we care more about whether things <em>evaluate</em> to true/false than whether they are literally <code>true</code> or <code>false</code>. This means that anything other than <code>nil</code> or <code>false</code> will be "truthy".</p></li> <li><p>The fact that your method depends entirely on information known to <code>User</code> objects indicates that it would be better to hang it off of those objects, for instance <code>current_user.follows? other_user</code>.</p></li> <li><p>You are duplicating behavior that would already be provided to you by using associations.</p></li> </ol> <p>Finally, taking all of these things into consideration, the answer:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :followings, :class_name =&gt; 'Following', :foreign_key =&gt; 'followed_id' has_many :followers, :through =&gt; 'followings' def follows?(other) other.followed_by? self end def followed_by?(other) followers.include? other end end </code></pre> <p><strong>NB</strong>: The use of the <code>followed_by?</code> method here is a use of <a href="http://en.wikipedia.org/wiki/Double_dispatch">double dispatch</a> that prevents the (minor) <a href="http://c2.com/cgi/wiki?LawOfDemeter">Law of Demeter violation</a> of one user knowing directly about the state of another user's followers. Rather, the first user object asks the second user object a direct question ("Are you followed by me?") and bases the result off of the answer. (It is also likely to be a useful method in and of itself.)</p>
    singulars
    1. This table or related slice is empty.
    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. 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