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
Now you can get the completed steps for a course as follows: