Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:26:11+00:00 2026-06-18T08:26:11+00:00

I have a class, Teacher, that has multiple integer columns: t.integer :a_students t.integer :b_students

  • 0

I have a class, Teacher, that has multiple integer columns:

t.integer :a_students
t.integer :b_students
t.integer :c_students
t.integer :d_students
t.integer :f_students

And I have a view that loops through all the teachers and states how many students are in their class. Now although I could add all the students in the view, just to make it look nicer I want to offload adding all the students into the model. I have the following model method:

  def count_students( teacher_id )
    t = Teacher.find(teacher_id)
    total = t.a_students + t.b_students + t.c_students + t.d_students + t.f_students
    total
  end

If I’m not mistaken, I shouldn’t be accessing model methods from the view – that should be left to the controller. But since its looping through, I’m a little lost on what to do in this case. Any Ideas?

Thanks
Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:26:12+00:00Added an answer on June 18, 2026 at 8:26 am

    In your teacher model have a method something like this:

    def total_students
      a_students + b_students + c_students + d_students + e_students + f_students
    end
    

    Then in your view if you have t, a particular teacher, you can just do t.total_students to get the total you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that has id numbers, names, class period, and teacher name.
I'm in a class that has a labview course, the teacher gave us a
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
I have few models - User , Teacher and TeacherLeader . class User <
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
Lets say we have these classes: class Teacher { public int Id { get;
I already have a table created - Student list. It has columns namely, the
I have two entites, Person and Teacher , with a OneToMany relationship: public class
I have a student that has many courses. In the student#update action and form,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.