【发布时间】:2016-07-04 12:24:01
【问题描述】:
所以我对 rails 和 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.last.state_changes 时它工作正常。
我的客户.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_change.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
state.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
我还收到关于我在 client.rb 中定义的范围的语法错误。我遵循了ActiveRecord 指南,但他们没有解释如何在实际范围查询中使用链接方法。
【问题讨论】:
标签: ruby-on-rails ruby activerecord scope