如何选择多个条件以返回符合这些条件的一个对象。

huangapple go评论59阅读模式
英文:

How to select for multiple conditions to return one object that matches those conditions

问题

我正在创建一个测试,并试图创建一个方法,当匹配职位标题、职位类型和描述时,可以提取特定的职位ID,以防有多个具有相同标题的职位。

我无法使select语句从实例变量中提取职位ID。调试显示,确实在实例变量中嵌套了ID,但因为我没有正确操作条件,所以条件没有被满足。

@job_posting是包含我需要的ID的实例变量,但我需要我的参数在select中匹配,以便随后返回ID。

当我只使用职位标题时,例如:

target_postings = @job_postings.select { |posting| posting[:posting_title] == posting_title }

它可以工作并返回我需要的ID,然而我不能这样做:

def get_specific_posting_id_for_posting(posting_title, job_type, description)
  expect(@job_postings.length > 0)
  target_postings = @job_postings.select { |posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description }
  expect(target_postings.length == 1)
  target_posting = target_postings[0]
  posting_id = target_posting[:posting_id]
  posting_id
end
英文:

I am creating a test, and am trying to create a method that pulls a specific job posting ID when it matches the job posting title, the job type and the description, just in case that the case that there are more than one job posting with the same title.

I cannot get the select statement to pull the job posting ID out of the instance variable. Debugging shows that there is indeed the ID nested in the instance variable, but my conditions aren't being met because I am not doing it correctly.

@job_posting is the instance variable that contains the ID that I need, but I need my parameters in select to match so I can subsequently return the ID.

whenever I ONLY use posting title,such as:

target_postings = @job_postings.select{|posting|posting[:posting_title]}

it works and returns the ID I need, however I cannot do this:

def get_specific_posting_id_for_posting(posting_title, job_type, description)
  expect(@job_postings.length > 0)
  target_postings = @job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}
  expect(target_postings.length == 1)
  target_posting = target_postings[0]
  posting_id = target_posting[:posting_id]
  posting_id
end

答案1

得分: 4

It looks like

target_postings = @job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}

should probably be

target_postings = @job_postings.select do |posting| 
  posting[:posting_title] == posting_title 
    && posting[:job_type] == job_type 
    && posting[:description] == description
end

Your version has three separate checks, the first two of which do nothing, only the last statement in the block is actually being used to determine whether the item matches.

As an aside, since it looks like you only want the single first element that matches your conditions, you might want to consider using find instead of select. It works the same except it will stop iterating and return as soon as it finds the first matching item.

英文:

It looks like

target_postings = @job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}

should probably be

target_postings = @job_postings.select do |posting| 
  posting[:posting_title] == posting_title 
    && posting[:job_type] == job_type 
    && posting[:description] == description
end

Your version has three separate checks, the first two of which do nothing, only the last statement in the block is actually being used to determine whether the item matches.

As an aside, since it looks like you only want the single first element that matches your conditions, you might want to consider using find instead of select. It works the same except it will stop iterating and return as soon as it finds the first matching item.

huangapple
  • 本文由 发表于 2020年1月3日 22:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定