Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 验证关联中的唯一性_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 验证关联中的唯一性

Ruby on rails 验证关联中的唯一性,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,鉴于以下类别: class Candidate has_many :applications has_many :companies, :through => :job_offers end class JobOffer belongs_to :company end class Application belongs_to :candidate belongs_to :job_offer end 如何在Rails上验证前面的语句(图中) 更新时,在应用程序上添

鉴于以下类别:

class Candidate
  has_many :applications
  has_many :companies, :through => :job_offers
end

class JobOffer
  belongs_to :company
end

class Application
  belongs_to :candidate
  belongs_to :job_offer
end

如何在Rails上验证前面的语句(图中)

更新时,在应用程序上添加以下验证将不起作用:

def validate_uniqueness_of_candidate_within_company
  errors.add(:job_offer_id, "...") if candidate.companies.include?(company)
end
原因当尝试将申请更改为同一公司候选人的不同职务时。公司将返回该公司

我还尝试在应用程序中执行类似的操作:

validates_uniqueness_of :user_id, :scope => {:job_offer => :company_id}
但它也不起作用。
有没有办法不用10行蹩脚的代码就能解决这个问题?

我将这样构造模型:

class Candidate
  has_many :applicatons
  has_many :job_offers
  has_many :offering_companies, :through => :job_offers
end

class Application
  belongs_to :candidate
end

class JobOffer
  belongs_to :candidate
  belongs_to :company

  validates_uniqueness_of :candidate_id, :scope => :company_id
end

class Company
end
这应该管用!
顺便说一句,为了避免命名混乱,我会将该应用程序称为其他名称(rails已经有application.rb

如图所示,一个应聘者和一份工作邀请将有许多应用程序&每个公司只能申请一份工作邀请。因此,验证应用程序中工作邀请范围内的应聘者的唯一性可能就行了

class Candidate
  has_many :applications
  has_many :job_offers, :through => :applications
end

class JobOffer
  belongs_to :company
  has_many :applications
  has_many :candidates, :through => :applications
end

class Application
  belongs_to :candidate
  belongs_to :job_offer

  validates_uniqueness_of :candidate_id, :scope => :job_offer_id
end
这将防止应用程序与同一职位的同一候选人关联。该候选人可以申请其他职位,对吗

正如@ez所指出的,将应用程序重命名为合适的名称更好


希望这能有所帮助。

可能有很多可以接受的方法来解决您的问题,但我认为底线是您试图对表强制一个唯一性约束,该约束(直接)不具有所有属性(包括
公司
用户
)。我会将
公司
信息反规范化到应用程序表(
用户id
工作职位id
公司id
)中,并在保存之前始终在
回调中设置它,以匹配
工作职位
公司
。然后您应该能够使用范围唯一性验证:

class JobApplication < ActiveRecord::Base
  belongs_to :user
  belongs_to :job_offer
  belongs_to :hiring_company, :class_name=>"Company", :foreign_key=>"company_id"

  before_save :set_hiring_company

  def set_hiring_company
   self.hiring_company = job_offer.hiring_company
  end

  validates_uniqueness_of :user_id, :scope => :company_id
end
class JobApplication“公司”;外键=>“公司id”
保存前:设置招聘公司
def set_招聘公司
self.employing\u company=工作邀请。employing\u company
结束
验证:user\u id,:scope=>:company\u id的唯一性
结束

适用于今天发现此问题的任何人。答案是检查是否存在预先存在的(防止多个)&在应用程序本身切换时跳过

def validate_uniqueness_of_candidate_within_company
  pre_existing = Application.includes(:candidate).includes(job_offer: :company).
    where(companies: { id: self.company.id }).
    where(candidates: { id: self.candidate.id })

  if pre_existing.present? && (!pre_existing.map(&:id).include? self.id)
    errors.add(:job_offer_id, "...")
  end
end

为什么这份工作机会属于一个应聘者这份工作机会只属于公司我想你误解了图片中的图表。不过谢谢你的回答!这不太正确。正如图片上所说,一个应聘者不能申请同一家公司的两份工作机会。这只会阻止应聘者申请同一份工作呃两次..你的答案是正确的。虽然我不想为了一个简单的验证而去规范化数据..谢谢!还有其他想法吗?你可以运行不同的验证:on=>:create而不是:on=>:update,但我仍然认为你最终会在连接表中循环使用该属性。我想看看其他解决方案对我来说,这是最简单的解决方案(没有10行恶心的话);-)祝你好运。现在有更好的解决方案吗?