1

Tutor モデルに act_as_tggable_on gem を使用しました。各チューターには、クラス (C201、M101)、科目 (化学、数学など)、背景 (芸術、科学) など、複数のタイプのタグ付けがあります。

生徒はその順番でチューターとマッチングしたいと考えています。例えば:

クラスが完全に一致する家庭教師が 10 人いる場合は、停止します。

クラスが完全に一致する家庭教師が 5 人しかいない場合は、科目が一致する次の 5 人を見つけます。

科目が完全に一致する講師が 2 人しかいない場合は、背景が一致する次の 3 人を見つけます。

スコープまたは SQL クエリを効率的に記述するにはどうすればよいですか? これを行う単純な方法は、生徒ごとに、すべてのチューターの関連性を計算し、それに応じてランク付けする必要があります。しかし、これはあまりにも非効率的です。

4

2 に答える 2

1

Tutors.where(:class => 'value', :subject => ....).order_by(:class, :subject, ...).limit(10) のようなもの

そして、マッチの組み合わせを覚えますか?

于 2012-05-17T21:00:15.787 に答える
1

モデル:

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

タグのマッチング:

# 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
于 2012-05-17T21:35:40.087 に答える