Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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 4 为什么Rails 4的作用域显示为方法_缺失?_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 为什么Rails 4的作用域显示为方法_缺失?

Ruby on rails 4 为什么Rails 4的作用域显示为方法_缺失?,ruby-on-rails-4,Ruby On Rails 4,我确信我忽略了一些显而易见的东西,但是当我试图调用它们时,我在这个模型中的作用域会产生一个method_missing错误。例如,在新的Rails控制台中 inspection = Inspection.find(1) inspection.created_before NoMethodError: undefined method `created_before` 我从Rails指南中复制了这个示例,希望我犯了语法错误。我简化了它,这样它就不需要变量了。我已经注释了所有内容,但是类定

我确信我忽略了一些显而易见的东西,但是当我试图调用它们时,我在这个模型中的作用域会产生一个method_missing错误。例如,在新的Rails控制台中

 inspection = Inspection.find(1)
 inspection.created_before

 NoMethodError: undefined method `created_before`
我从Rails指南中复制了这个示例,希望我犯了语法错误。我简化了它,这样它就不需要变量了。我已经注释了所有内容,但是类定义和范围没有区别。其他型号使用示波器,但这一款我看不出我做错了什么。我做了一个类定义,看看它是否是,但它不起作用。这个类中的其他方法似乎工作得很好。我花了一整天的时间在这上面,没有看到它,也没有寻求帮助

检查.rb

VARIANCE_ITEMS = 60
MISC_ITEMS = 70
REVIEW_ITEMS = [20, 30, 40, 50]


class Inspection < ActiveRecord::Base
  resourcify

  include RollbackLogger
  include SurveyHelper

  belongs_to :site
  belongs_to :survey

  has_many :scores, dependent: :destroy
  accepts_nested_attributes_for :scores

  has_one :cash_variance
  has_one :message, dependent: :destroy

  scope :created_before, -> { where("created_at < ?", Time.now) }
  scope :recent_of_same_type, ->(survey){ where(survey_id: survey.id).order("inspection_date desc").limit(5) }



  before_create do |inspection|
    begin
    inspection.name = Survey.find(inspection.survey_id).name

    rescue ActiveRecord::RecordNotFound
      inspection.name = 'blank'
    end
    unless inspection.inspection_date?
      inspection.inspection_date = inspection.created_at
    end
  end

  after_create do |inspection|
    items = Item.where(survey_id: inspection.survey_id)
    items.each do |item|
      Score.create(score_item: item.high_score, item_id: item.id, inspection_id:     inspection.id, multiplier: item.scoring)
    end
    survey = Survey.find(inspection.survey_id)
    if survey && survey.name == "Loss Prevention"
      REVIEW_ITEMS.each do |x|
        3.times do |score|
          Score.create!(item_id: ("#{x}#{score}").to_i, inspection_id: inspection.id, multiplier: 1)
        end
      end

    end

  end



protected

  def message_check
    actual_score = grand_total(self.scores, :score_item, :multiplier)
    hi_score = high_score_total(self.survey.items, :high_score)

    unless self.message.nil?
      if (actual_score < hi_score) && (self.message.emailed)
        self.message.update_attributes(flag: FLAGS[2]) if self.message
      elsif (actual_score < hi_score) && (self.message.emailed == false)
        self.message.update_attributes(flag: FLAGS[1]) if self.message
      else
        self.message.update_attributes(flag: FLAGS[0]) if self.message
      end
    end
  end

end
差异项目=60
其他项目=70
审查项目=[20,30,40,50]
类别检查{where(“在<?”创建,Time.now)}
经营范围:同一类型的近期检验,->(检验){其中(检验id:survey.id).order(“检验日期描述”).limit(5)}
创建检查前|
开始
inspection.name=Survey.find(inspection.Survey\u id).name
rescue ActiveRecord::RecordNotFound
inspection.name='blank'
结束
除非检查。检查日期?
inspection.inspection\u日期=inspection.created\u日期
结束
结束
创建后进行检查|
项目=项目。其中(调查编号:检验。调查编号)
项目。每个do |项目|
Score.create(Score\u item:item.high\u Score,item\u id:item.id,inspection\u id:inspection.id,乘数:item.Score)
结束
测量=测量.查找(检查.测量\u id)
如果survey&&survey.name==“损失预防”
审查| u项目。每个项目都要| x|
3.5倍不得分|
得分,创造!(项目id:(“#{x}#{score}”)。到#i,检查编号:inspection.id,乘数:1)
结束
结束
结束
结束
受保护的
def消息检查
实际分数=总分数(自我分数,:分数项目,:乘数)
hi_分数=hi_分数总计(self.survey.items,:hi_分数)
除非self.message.nil?
if(实际分数<高分数)&(self.message.email)
如果self.message,则更新_属性(标志:标志[2])
elsif(实际分数<高分数)&&(self.message.emailed==false)
如果self.message,则更新_属性(标志:标志[1])
其他的
如果self.message,则更新_属性(标志:标志[0])
结束
结束
结束
结束

范围是在类级别而不是实例级别定义的。因此,您的范围可以通过调用

Inspection.created_before
scope
只是一种
class\u方法
创建
方法
它将为您定义这些方法

scope :method_name,->(variables_accepted){method body}
也就是说

class Inspection < ActiveRecord::Base
  def self.method_name(variables_accepted)
      method body
  end
end
称为

Inspection.created_before_id(1)
这将返回在给定
id的创建日期之前创建的所有
检查
注意如果找不到
id
,这将引发。你可以这样改变它

scope :created_before_id, ->(id){ 
                    inspection = find(id)
                    inspection ? where("created_at < ?", inspection.created_at) : scoped   
}
它将返回给定实例之前的所有
检查
s,就像您现在所做的一样

Inspection.find(1).created_before

是的,我看得更清楚。我试图调和相关模型分数中类似范围的使用方式。ie:inspection.scores.my_cool_scope按预期工作,但在检查本身时不会工作。@sam452工作的原因是范围链接。实际上,您正在调用
Scores.where(inspection\u id:YOUR\u INSEPCTION\u id)。my\u cool\u scope
,这样您就可以通过
ActiveRecord::Relation
对象看到它仍然保持在类级别。您当前的实现创建并实例
检查的变量
,从而保留较高的级别。为了更加清晰,我在回答中添加了一些内容。
def created_before
  Inspection.where("created_at > ?", self.created_at)
end
Inspection.find(1).created_before