Rails范围与方法值比较

时间:2016-07-04 12:24:02

标签: ruby-on-rails ruby activerecord scope

所以我对rails和$scope.results = [ {id: 1, text: 'a'}, {id: 2, text: 'b'}, {id: 3, text: 'c'}, {id: 4, text: 'd'} ]; <label ng-repeat="result in results"> <input type="checkbox" name="res" data-checklist-model="my.resid" data-checklist-value="result.id" ng-click="myFunction(result.id)" > {{result.text}} </label> $scope.myFunction = function(id){ $scope.dflt = $scope.dflt || []; if($scope.my.resid == 1){ $scope.dflt.push({"id": 1, "text": 'a'}); console.log($scope.dflt); } } 相当陌生,我需要在ActiveRecord个实体之间进行过滤。基本上,范围应返回客户端当前状态等于某个状态对象的所有Client条记录。

这是通过获取客户的最后Client然后提取state_change state_change from_state对象来计算的。

我已经定义了一个方法来返回State,但是当我用current_state测试时,它在rails控制台中我收到此错误:

Client.current_state(Client.last)但在控制台中运行NameError: undefined local variable or method 'state_changes for #<Class:0x0000000685eb88>时效果很好。

我的client.rb

Client.last.state_changes

state_change.rb

class Client < ActiveRecord::Base
  has_and_belongs_to_many               :users
  belongs_to                            :industry
  belongs_to                            :account
  has_many                              :contacts
  has_many                              :state_changes
  belongs_to                            :head,          class_name: "Client"
  has_many                              :branches,      class_name: "Client", foreign_key: "head_id"
  has_many                              :meetings,      through: :contacts
  has_many                              :sales,         through: :meetings

  scope :prospects, -> (client) { where(Client.current_state(client): State.PROSPECT_STATE) }

  def self.has_at_least_one_sale? (client)
    return client.sales.empty?
  end

  def self.has_account_number? (client)
    return client.account_number.present?
  end

  def self.current_state (client)
    state_changes.last.to_state
  end
end

state.rb

class StateChange < ActiveRecord::Base
  belongs_to :client
  belongs_to :from_state,   class_name: "State", foreign_key: :to_state_id
  belongs_to :to_state,     class_name: "State", foreign_key: :from_state_id
end

我也遇到了关于我在client.rb中定义的范围的语法错误。我已经按照class State < ActiveRecord::Base has_many :from_states, class_name: "StateChange", foreign_key: :to_state_id has_many :to_states, class_name: "StateChange", foreign_key: :from_state_id def self.PROSPECT_STATE return State.find_by name: 'Prospect' end def self.CLIENT_STATE return State.find_by name: 'Client' end def self.SUSPECT_STATE return State.find_by name: 'Suspect' end end 指南进行了操作,但他们没有解释如何在实际范围查询中使用链式方法。

1 个答案:

答案 0 :(得分:2)

您收到错误NameError: undefined local variable or method 'state_changes for #<Class:0x0000000685eb88>的原因是因为您将current_state定义为类方法并将客户端作为参数传递。这就是为什么在类而不是实例上调用state_changes的原因。在这种情况下,您需要使用客户端来获取state_changes

def self.current_state (client)
  client.state_changes.last.to_state
end

此外,范围仅用于链接查询逻辑。我不确定是否可以使用查询来获得您想要的结果。我希望我能正确理解你的逻辑。或者,您可以使用类方法。

def self.prospects (client)
  Client.all.select { |c| c.current_state(c) == State.PROSPECT_STATE }
end

正如Зелёный在评论中所指出的,也许你也想把方法改为实例方法,在这种情况下阅读他所链接的资源会非常有帮助。

根据评论更新:

我认为您真正想要的是使用current_state的实例方法,如下所示:

def current_state
  state_changes.last.to_state
end

然后你可以得到这样的潜在客户:

def self.prospects
  Client.all.select { |c| c.current_state == State.PROSPECT_STATE }
end