ruby - 在 Ruby on Rails 应用程序中实现匹配算法

标签 ruby ruby-on-rails-3 algorithm matching

<分区>

我已经建立我的工作委员会大约 10 个月了。我为用户提供了搜索和申请的能力,但我认为有必要为他们提供匹配结果。我知道有很多方法可以进行匹配,但最有效的方法是什么。是继续把匹配算法写在用户模型里,还是做成模块,还是做成插件?目前我已经在模型中启动了它,但它似乎会变得非常困惑。

class User < ActiveRecord::Base


  attr_accessible :name, :provider, :uid, :bio, :gender, :location, :interests, :education, :jobs, :state, :city, :resume, :available, :website, :video_cover, :date_of_birth,
 :language, :skills

 class Job < ActiveRecord::Base

  attr_accessible :title, :company, :company_description, :industry, :requisition_number, :city, :state, :zipcode, :posted_ate, :experience, :job_type, :longitude, :latitude,  :recruiter_id, :apply_link, :contact_name, :contact_email, :job_id

def self.search(search)
  if search
    find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
 end

我应该扩展搜索方法并在模型用户和工作模型中创建匹配算法还是将其移动到模块中?

最佳答案

我的意见 - 如果您看到功能在增长,请将其提取到一个类中。像这样:

class JobFilter
  def initialize(scope)
    @scope = scope
  end

  def filter(params)
    filter_by_title params[:title]
    ...
    @scope
  end

  private

  def filter_by_title(title)
    @scope = @scope.where('title LIKE ?', "%#{title}%")
  end

  ...
end

然后,您可以这样调用它:matching_jobs = JobFilter.new(Job.all).filter({ title: 'CTO' })

过滤的细节集中在一个位置,您的用户/工作模型不会随着您添加越来越多的匹配参数而变得困惑。作为奖励,它更容易测试。

关于ruby - 在 Ruby on Rails 应用程序中实现匹配算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23870722/

相关文章:

ruby-on-rails - rails 中的验证在内部使用元编程?它实际上是如何工作的?

ruby-on-rails - 用纯 Ruby 编写 Rails 模板

python - 如何检查列表的元素是否连续

algorithm - null 是二叉树吗?

ruby - pythonchallenge.com 4 in ruby​​ 解决方案

ruby-on-rails - 使用 Rspec v.1 测试响应 cookie

sql - 在 Ruby、Activerecord 或 Postgresql 中按周对事件进行分组和计数

ruby-on-rails - Rails 4 销毁操作,undefined_method message_path

algorithm - 树中最长路径的公共(public)段

ruby - 使用 DOM 检查器查找按钮的 ID