Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex model relationships compared to writing methods
    text
    copied!<p>I am adding a voting and karma/reputation system (similar to here, HN, etc.) for an app and have decided to make my own as none of the existing gems had all the features I wanted. While working on the basic functionality I ended up writing some pretty complex associations in my models, and am unsure how this compares to say, writing my own methods (or scopes) to return the same data.</p> <p>Pseudo-code below. (Users can Vote on Questions and Answers, but Users also have upvotes and downvotes through Q/A that they've posted)</p> <pre><code>User has_many :questions has_many :answers has_many :votes has_many :question_up_votes, through: :questions, source: :vote, conditions: {voteable_type: :question, 'score &gt; 0'} has_many :question_down_votes, through: :questions, source: :vote, conditions: {voteable_type: :question, 'score &lt; 0'} has_many :answer_up_votes, through: :answers, source: :vote, conditions: {voteable_type: :answer, 'score &gt; 0'} has_many :answer_down_votes, through: :answers, source: :vote, conditions: {voteable_type: :answer, 'score &lt; 0'} Question belongs_to :user has_many :questions has_many :votes, as: :voteable Answer belongs_to :user belongs_to :question has_many :votes, as: :voteable Vote belongs_to :voter, class_name: "User" belongs_to :voteable, polymorphic: true belongs_to :user, through: :voteable attr_accessible :score, :user_id, :voteable_type, :voteable_id </code></pre> <p>Specifically, for the four complex associations in the User model, would it be better performance, less error-prone, or have other advantages to write some methods (or scopes) that return the same information, either in the User model or the Q/A models?</p> <p>I'm very new to Rails (and programming) and this is my first Stack Overflow question, so gentleness appreciated. Let me know if this question is a better fit for a different Stack property. Thanks!</p>
 

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