Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails中按顺序搜索标记_Ruby On Rails_Sorting - Fatal编程技术网

Ruby on rails 在Rails中按顺序搜索标记

Ruby on rails 在Rails中按顺序搜索标记,ruby-on-rails,sorting,Ruby On Rails,Sorting,我在我的导师模型中使用了gem上的acts_as_taggable_。每个导师都有多种类型的标记:课程(C201、M101)、科目(化学、数学等)和背景(艺术、科学) 学生们希望按照这个顺序与导师进行匹配。例如: 如果有10名导师的课程完全匹配,那么就停止 如果只有5名导师与课程完全匹配,那么找到下5名与科目匹配的导师 如果只有2名导师的科目完全匹配,那么接下来的3名导师的背景匹配 如何有效地编写作用域或SQL查询?一个简单的方法是,对于每个学生,我必须计算所有导师的相关性,并对他们进行相应的排

我在我的导师模型中使用了gem上的acts_as_taggable_。每个导师都有多种类型的标记:课程(C201、M101)、科目(化学、数学等)和背景(艺术、科学)

学生们希望按照这个顺序与导师进行匹配。例如:

如果有10名导师的课程完全匹配,那么就停止

如果只有5名导师与课程完全匹配,那么找到下5名与科目匹配的导师

如果只有2名导师的科目完全匹配,那么接下来的3名导师的背景匹配


如何有效地编写作用域或SQL查询?一个简单的方法是,对于每个学生,我必须计算所有导师的相关性,并对他们进行相应的排名。但这太没效率了。

所以有点像导师。其中(:class=>‘value’,:subject=>…)。排序依据(:class,:subject…)。限制(10)

然后记忆匹配的组合?

模型:

require 'active_record'

ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false

ActiveRecord::Base.establish_connection(
    :adapter => 'sqlite3',
    :dbfile  => ':memory:'
)

ActiveRecord::Schema.define do

    create_table :College do |table|
        table.column :name, :string
    end

end

class College < ActiveRecord::Base
    acts_as_taggable_on :roles, :courses, :subjects, :backgrounds
end

college = College.new(:name => 'Kerry Green', :role_list => 'student', :course_list => 'CM201', :subject_list => 'Biochemistry', :background_list = 'Science')
college.save

college = College.new(:name => 'Brian Jones', :role_list => 'student', :course_list => 'CM101', :subject_list => 'Chemistry', :background_list = 'Science')
college.save

college = College.new(:name => 'Lewis Smith', :role_list => 'student', :course_list => 'AR102', :subject_list => 'Fine Art', :background_list = 'Arts')
college.save

college = College.new(:name => 'Evan Hunt', :role_list => 'tutor', :course_list => 'CM201, CM101', :subject_list => 'Chemistry, Biochemistry', :background_list = 'Science')
college.save

college = College.new(:name => 'Stuart White', :role_list => 'tutor', :course_list => 'CM201', :subject_list => 'Biochemistry', :background_list = 'Science')
college.save

college = College.new(:name => 'Wendy Jones', :role_list => 'tutor', :course_list => 'CM201', :subject_list => 'Biochemistry', :background_list = 'Science')
college.save

模型之间的关系是什么?您能展示一些示例数据来编写SQL查询吗?谢谢beck和Ayush。请看我更新的问题。
# find CM201 courses

@student = College.find( :name => 'Brian Jones')

@byCourse = @student.find_related_on_courses.find_tagged_with('tutor', :on=> :roles) 
 if @byCourse.length >= 10 
    // rule 1
else  
  @bySubject = @student.find_related_on_subjects.find_tagged_with('tutor', :on=> :roles) 
  if @byCourse.length >= 5 && @bySubject.length >= 5
   // rule 2
  else
     @byBackground = @student.find_related_on_backgrounds.find_tagged_with('tutor', :on=> :roles) 
     if @bySubject.length >= 2 && @byBackground.length >= 3
        // rule 3
     else
       // no match
     end
  end
end