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 7559885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:45:08+00:00 2026-05-30T12:45:08+00:00

I am trying to write a function completed_steps_for_course(course) in my User model that finds

  • 0

I am trying to write a function completed_steps_for_course(course) in my User model that finds all the completed steps for a course the user has done.

My User model is below where I want the function to live

class User < ActiveRecord::Base

  has_many :memberships, :dependent => :destroy
  has_many :groups, :through => :memberships
  has_many :enrolments, :through => :groups
  has_many :courses, :through => :enrolments
  has_many :completed_steps, :dependent => :destroy

  accepts_nested_attributes_for :memberships, :allow_destroy => true
  attr_accessible :email, :password, :password_confirmation, :memberships_attributes, :is_admin,
              :first_name, :last_name, :gender, :dob, :phone_number, :address, :city, :state, :postcode

  def started_course?(course)
    completed_steps_for_course(course).length > 0
  end

  def completed_steps_for_course(course)
    #step_in_course = Course.joins(:components => :steps).where("course_id = ?", course.id)
    #completed_steps.where("course_id = ?", course.id)
  end
end

My models for Course, Component and Step are below…

class Course < ActiveRecord::Base
  has_many :components, :dependent => :destroy
  has_many :steps, :through => :components, :dependent => :destroy
end

class Component < ActiveRecord::Base
  belongs_to :course
  has_many :steps, :dependent => :destroy
end

class Step < ActiveRecord::Base
  belongs_to :component
end

I cannot think how the function should look, please help! I realize this is probably an easy question but the morning coffee obviously has not helped today.

EDIT I have a table that monitors the completed steps for a User as per below…

class CompletedStep < ActiveRecord::Base
  belongs_to :user
  belongs_to :step
  validates_presence_of :user, :step
  validates_uniqueness_of :step_id, :scope => 'user_id'
end
  • 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-30T12:45:10+00:00Added an answer on May 30, 2026 at 12:45 pm
    class User < ActiveRecord::Base
      #
      # .. other associations
    
      has_many :completed_steps, :dependent => :destroy do
        def for_course(course)
          joins(:step=> {:component => :course}).where("courses.id = ?", course)
        end
      end 
    end
    

    Now you can get the completed steps for a course as follows:

    current_user.completed_steps.for_course(c1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a
I'm trying to write a function that is able to determine whether a string
I am trying to write a function in C++ that solves for X using
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a C++ function that matches whether a string is
I am trying to write a very simple slide up and slide function that
I am trying to write a translation function that takes the value and the
I'm trying to write a very simple function which displays a div that says

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.