检查一个字段是否包含在 ROR 的另一个列表中
Check a field is contains in another list in ROR
我有一个名为 @domianskill_technical_skills
的列表。此 table 与 domain_skills 和 technical_skills 数据相关 table 并基于检索在 domain_skill_id
我需要 return technical_skills table 中的所有 technical_skills,但还有一个属性名为 assigned 如果 technical_skill_id
出现在 @domianskill_technical_skills
列表中,则为真。
我的工作代码如下
def assigned_technical_skills
@domain_skill_technical_skills = DomainSkillsTechnicalSkill.select('id, technical_skill_id').where(domain_skill_id: params[:domain_skill_id])
Rails.logger.info(@domain_skill_technical_skills.to_a)
TechnicalSkill.all.where(record_status: 1).each do |skill|
if @domain_skill_technical_skills.include?(skill.id) then
Rails.logger.info(skill.id.to_s + skill.name)
end
end
respond_with @domain_skill_technical_skills
end
请指导我更正 if 语句,还需要 select 所需的字段(id、名称、分配)并为 return 创建一个实例。
如果 DomainSkillsTechnicalSkill 有 'technical_skill_id' 列
,您可能想用这个替换您的代码
def assigned_technical_skills
# just collect technical_skill_id
@domain_skill_technical_skill_ids = DomainSkillsTechnicalSkill.select('id, technical_skill_id').where(domain_skill_id: params[:domain_skill_id]).collect(&:technical_skill_id)
Rails.logger.info(@domain_skill_technical_skill_ids.to_a)
@technical_skills = []
TechnicalSkill.select('id, name').where(record_status: 1).each do |skill|
@technical_skills << skill.as_json.merge!(
{ assigned: @domain_skill_technical_skill_ids.include?(skill.id)}
)
end
respond_with @technical_skills
end
我有一个名为 @domianskill_technical_skills
的列表。此 table 与 domain_skills 和 technical_skills 数据相关 table 并基于检索在 domain_skill_id
我需要 return technical_skills table 中的所有 technical_skills,但还有一个属性名为 assigned 如果 technical_skill_id
出现在 @domianskill_technical_skills
列表中,则为真。
我的工作代码如下
def assigned_technical_skills
@domain_skill_technical_skills = DomainSkillsTechnicalSkill.select('id, technical_skill_id').where(domain_skill_id: params[:domain_skill_id])
Rails.logger.info(@domain_skill_technical_skills.to_a)
TechnicalSkill.all.where(record_status: 1).each do |skill|
if @domain_skill_technical_skills.include?(skill.id) then
Rails.logger.info(skill.id.to_s + skill.name)
end
end
respond_with @domain_skill_technical_skills
end
请指导我更正 if 语句,还需要 select 所需的字段(id、名称、分配)并为 return 创建一个实例。
如果 DomainSkillsTechnicalSkill 有 'technical_skill_id' 列
,您可能想用这个替换您的代码def assigned_technical_skills
# just collect technical_skill_id
@domain_skill_technical_skill_ids = DomainSkillsTechnicalSkill.select('id, technical_skill_id').where(domain_skill_id: params[:domain_skill_id]).collect(&:technical_skill_id)
Rails.logger.info(@domain_skill_technical_skill_ids.to_a)
@technical_skills = []
TechnicalSkill.select('id, name').where(record_status: 1).each do |skill|
@technical_skills << skill.as_json.merge!(
{ assigned: @domain_skill_technical_skill_ids.include?(skill.id)}
)
end
respond_with @technical_skills
end