Rails:查找重叠的记录

时间:2015-01-25 20:06:29

标签: ruby-on-rails ruby ruby-on-rails-4 associations overlap

我有一个has_many :fonctions的工作模式,在我的工作模式中,我创建了一种方法,可以在给定的工作中提供与所有工作相比的类似功能。

例如:我想将我的所有工作与Job1工作1进行比较有这个功能("策略","管理","营销",企业家精神" ) 另一份工作2有这个功能("策略","管理","数据科学")

所以这必须在做(job1& job2).size 2

时给我

为此我在我的工作模型中有这个方法必须做类似的工作,但问题是我得到这个错误undefined local variable or method for job'

  def fonctions_score
    (job.fonctions.collect(&:id) & self.fonctions.collect(&:id)).size
  end

更新

这是我现在尝试但仍然收到此错误的代码wrong number of arguments (0 for 1)

def fonctions_score(other_job)
  these = other_job.fonctions.collect {|f| f.id } 
  those = self.fonctions.collect {|f| f.id } 
  logger.debug these logger.debug those # should just have lists of ids 
  common = (these & those) 
  logger.debug common # should be common ids 
  common.size
end

在我的控制器中我订购了这样的工作

@related_jobs = Job.all
@related_jobs.sort_by do |related_job|
  related_job.final_score
end

1 个答案:

答案 0 :(得分:0)

试试这个

def calculate(j1, j2)
  (j1.fonctions.pluck(:id) & j2.fonctions.pluck(:id)).size
end

def calculate
   self.fonctions.count
end