title: Rails - How to write a sum of two model attributes
body: <p>I'm trying to make an app with Rails 4.</p>
  
  <p>I have 3 models.</p>
  
  <pre><code>Project.rb
  ProjectInvitation.rb
  ProjectStudentEoi.rb
  </code></pre>
  
  <p>The associations are:</p>
  
  <pre><code>Project has may project_invitations
  Project has many project_student_eois
  
  ProjectInvitation belongs to project
  ProjectStudentEoi belongs to project
  </code></pre>
  
  <p>I'm trying to count how many students are participating in a project. They can either be invited or express interest (if not invited).</p>
  
  <p>In my project.rb, I tried to write a method to sum the count of accepted invitations or approved expressions of interest.</p>
  
  <p>In project.rb, I have:</p>
  
  <pre><code>  def self.students_participating
  
         ProjectStudentEoi.interested_students.sum.
  ProjectInvitation.invitations_accepted
    end
  </code></pre>
  
  <p>Note: I only split the above line into two lines because SO won't indent this long a line. It's all one continuous line in my code.</p>
  
  <p>In my projectInvitation.rb I have:</p>
  
  <pre><code>def self.invitations_accepted
      @project.project_invitations.where(student_accepted: true)
    end
  </code></pre>
  
  <p>In my ProjectStudentEoi.rb, I have:</p>
  
  <pre><code>    def self.interested_students
          @project.project_student_eoi.where
  (creator_accepted: true).count
      end
  </code></pre>
  
  <p>Again, the above code is split in 2 lines in this post because SO won't indent it as code when its this long. </p>
  
  <p>Then in my projects view folder, I have a partial, which has:</p>
  
  <pre><code>&lt;%= @project.students_participating %&gt; students participating 
  
  NoMethodError at /projects/2
  undefined method `students_participating' for #&lt;Project:0x007ff733333830&gt;
  </code></pre>
  
  <p>How do you write a sum of two count methods in rails?</p>
  
  <p>I tried the example given in the answer below - so in my Project.rb:</p>
  
  <pre><code>def students_participating
     project_student_eois.interested_students.sum + 
     project_invitations.invitations_accepted
    end
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>NoMethodError at /projects/2
  undefined method `interested_students' for #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
  </code></pre>
  
  <p>Picking up on Meier's suggestions below, I tried to rewrite my class methods as scopes, so in ProjectStudentEoi.rb, I have:</p>
  
  <pre><code>  scope :creator_accepted, lambda { where(creator_accepted: true)}
  </code></pre>
  
  <p>In ProjectInvitation.rb I have</p>
  
  <pre><code>scope :student_accepted, lambda { where(student_accepted: true)}
  </code></pre>
  
  <p>Then in project.rb (adapting Meier's suggestion), I have:</p>
  
  <pre><code>def students_participating
  
      project_student_eois.creator_accepted.count + 
      project_invitations.students_accepted.count
    end
  </code></pre>
  
  <p>I don't have a count function in the child objects any more.</p>
  
  <p>When i try this, I get this error:</p>
  
  <pre><code>NoMethodError at /projects/2
  undefined method `creator_accepted' for #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
  </code></pre>
  
  <p>THEN - taking Meier's revised suggestion, I tried:</p>
  
  <pre><code> def students_participating
  
      project_student_eois.creator_accepted + 
      project_invitations.students_accepted.count
    end
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>NoMethodError at /projects/2
  undefined method `creator_accepted' for #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
  </code></pre>