Ruby on rails 查找步骤id';那是属于一门课程的吗?

Ruby on rails 查找步骤id';那是属于一门课程的吗?,ruby-on-rails,ruby,ruby-on-rails-3,postgresql,Ruby On Rails,Ruby,Ruby On Rails 3,Postgresql,我试图在我的用户模型中为课程(course)编写一个函数completed_steps_,用于查找用户完成的课程的所有已完成步骤 我的用户模型在下面,我希望该函数能够在这里使用 class User < ActiveRecord::Base has_many :memberships, :dependent => :destroy has_many :groups, :through => :memberships has_many :enrolments, :t

我试图在我的用户模型中为课程(course)编写一个函数completed_steps_,用于查找用户完成的课程的所有已完成步骤

我的用户模型在下面,我希望该函数能够在这里使用

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
class用户:销毁
拥有多个:组,:至=>:成员身份
有许多:注册人数,:通过=>:组
有很多:课程,:通过=>:注册
已完成多个步骤,:dependent=>:destroy
接受:memberships的\u嵌套的\u属性\u,:allow\u destroy=>true
属性可访问:电子邮件、:密码、:密码\u确认、:会员资格\u属性、:是\u管理员、,
:名字,:姓氏,:性别,:出生日期,:电话号码,:地址,:城市,:州,:邮政编码
def已开始课程?(课程)
已完成课程(课程)的课程步骤。长度>0
结束
def已完成课程的步骤(课程)
#课程中的步骤=课程。联接(:组件=>:步骤)。其中(“课程id=?”,课程id)
#已完成的课程步骤。其中(“课程id=?”,课程id)
结束
结束
我的课程、组件和步骤模型如下

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
课程:销毁
有多个:步骤,:到=>:组件,:依赖=>:销毁
结束
类组件:销毁
结束
类步骤
我想不出这个函数应该是什么样子,请帮忙!我意识到这可能是一个简单的问题,但今天早上的咖啡显然没有帮助

编辑我有一个表,可以为用户监控完成的步骤,如下所示

class CompletedStep < ActiveRecord::Base
  belongs_to :user
  belongs_to :step
  validates_presence_of :user, :step
  validates_uniqueness_of :step_id, :scope => 'user_id'
end
class CompletedStep“用户id”
结束

如何将
步骤
表链接到
用户
表?它没有明确链接,但将通过用户模型进行链接,该用户模型具有多个课程、多个组件和多个步骤。注册同一课程的两个不同用户可能处于不同的步骤中?如果上一个问题的答案是肯定的,那么您如何使用当前模型来表示呢?是的,这应该是可能的,用户只需查看步骤,并且随着每个后续步骤的进行,创建一个完整的步骤条目。此已完成的步骤条目将指定每个用户在课程中的位置。你认为这有什么问题吗?如果你想指出用户所处的当前步骤,你需要修改你的模型。您可以将
completed_steps
表重命名为
user_steps
,并引入一列来存储步骤的状态。
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
current_user.completed_steps.for_course(c1)