型號:
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
什麼模型之間的關係? –
你可以顯示一些示例數據來編寫SQL查詢嗎? – beck03076
謝謝貝克和Aayush。請看我更新的問題。 – AdamNYC