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

  • Home
  • SEARCH
  • 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 4081592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:04:50+00:00 2026-05-20T18:04:50+00:00

I have this Rails code in a method in a controller. It takes four

  • 0

I have this Rails code in a method in a controller. It takes four parameters, does a bit of math and I’d like it to return a the value stored in the ‘attendance’ variable to a partial in a view.

def find_attendance(lesson_id, student_id, start_date, end_date)
    marks = Array.new

    all_registers = Register.where(:lesson_id => lesson_id)

    start_date.upto(end_date) do |date|
        all_registers.each do |register|
            if date == register.date
                this_mark = RegisterMark.where(:student_id => student_id, :register_id => register.id)
                this_mark.each do |mark|
                    marks << mark.mark
                end
            end
        end
    end

    attendance = ((Float(marks.count("O")) + Float(marks.count("N"))) / Float(marks.count)) * Float(100)
end

I have created a _find_attendance.html.erb file in the relevant view directory, and tried calling the partial by using <%= render :partial => “find_attendance” %> but I’m having trouble passing parameters and using the correct variable name within the partial.

I’ve tried playing around with using instance variables on ‘attendance’.

Any pointers?

  • 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-05-20T18:04:51+00:00Added an answer on May 20, 2026 at 6:04 pm

    You almost certainly want to add this method as a method to the model. Maybe in your case the lesson model?

    class Lesson < ActiveRecord::Base
    
      has_many :registers
    
      def calculate_attendance_for_student(student, start_date, end_date)
        valid_dates = start_date.upto(end_date)   
        registers.each do |register|
        ...
      end
    end
    

    Then in your view you can get at the @lesson model to get attendance.

    There’s some area for improvement in your loops, I included a simple way to loop through all Register for a Lesson above. You can do this for both your Register.where and your RegisterMark.where for speed improvements

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

Sidebar

Related Questions

I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have never worked with web services and rails, and obviously this is something
I have a staging Rails site up that's running on MySQL 5.0.32-Debian. On this
I have this code in jQuery, that I want to reimplement with the prototype
I have this on line 56 in my environment.rb: I18n.load_path += Dir[ File.join(RAILS_ROOT, 'lib',
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the

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.