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 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 am creating a rails app and have used this code in one of
[Rails] Where to put this code? I have a user1 and when another registered
I have this command in a Rails controller open(source) { |s| content = s.read
I have a text like this: 'Rails on IIS7 http://is.gd/vWPn ' (it's a tweet
I'm developing a small application in Ruby-On-Rails. In a controller I have this piece
I have a Rails controller with a create method, and I want to POST
I have a controller that needs to return a 404 page and status code
This is the code I have at the end a the create method in
This Ruby on Rails problem is a bit vexing, as the code works fine
Ok so I have this relationship in Rails: class Position < ActiveRecord::Base belongs_to :company

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.