如何验证现场的存在?

时间:2016-10-19 12:16:49

标签: validation ruby-on-rails-4 activerecord

如果条件来自另一个嵌套?

,如何验证字段的存在?

answers_controller.rb

class AnswersController < CustomerTemplatesController
    def edit
        @answer = Answer.find(params[:id])
    end

    def update
        @answer = Answer.find(params[:id])
        if @answer.update_attributes(answer_params)
            redirect_to :action => :index
        else
            render :edit
        end
    end
end

answer.rb

class Answer < ActiveRecord::Base
    has_many :answer_detail, -> lambda{ includes(:template_details).references(:template_details).where.not(:template_details => {:id => nil}) }, :dependent => :destroy
    accepts_nested_attributes_for :answer_details, :allow_destroy => true
    validates_associated :answer_details
end

template.rb

class TemplateDetail < ActiveRecord::Base
    has_one :answer_detail
end

answer_detail.rb

class AnswerDetail < ActiveRecord::Base
    belongs_to :answer
    belongs_to :template_detail
    validates :name, :presence => true, :if => lambda { template_detail.isrequired }
end

这很有效!当我保存父Answer并且嵌套AnswerDetail中的名称为空时,会发生错误。我的问题是,在日志中,它看起来像这样;

(0.2ms)  BEGIN
TemplateDetail Load (0.6ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 1 LIMIT 1
TemplateDetail Load (0.2ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 2 LIMIT 1
TemplateDetail Load (0.7ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 3 LIMIT 1
TemplateDetail Load (0.3ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 4 LIMIT 1
TemplateDetail Load (0.2ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 5 LIMIT 1
TemplateDetail Load (0.3ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 6 LIMIT 1
TemplateDetail Load (0.7ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 7 LIMIT 1
TemplateDetail Load (0.6ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 8 LIMIT 1
TemplateDetail Load (0.8ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 9 LIMIT 1
TemplateDetail Load (0.7ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 10 LIMIT 1
TemplateDetail Load (0.6ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 11 LIMIT 1
TemplateDetail Load (0.4ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 12 LIMIT 1
TemplateDetail Load (0.5ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 13 LIMIT 1
TemplateDetail Load (0.7ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 14 LIMIT 1
TemplateDetail Load (0.2ms)  SELECT  `template_details`.* FROM `template_details` WHERE `template_details`.`id` = 15 LIMIT 1
(0.5ms)  ROLLBACK

我可以尽量减少这个吗?比如在AnswerDetail验证名称之前加载所有template_details。

0 个答案:

没有答案