これでレールがかなり新しくなりました。ActiveRecord
とClient
エンティティ間のフィルタリングの必要があります。基本的にスコープは、クライアントの現在の状態が特定の状態オブジェクトと等しいすべてのClient
レコードを返す必要があります。メソッドの値を比較したRailsスコープ
これを取得することによって計算され、クライアントのstate_change
最後に、その後引っ張っstate_change
のfrom_state
State
目的であること。
NameError: undefined local variable or method 'state_changes for #<Class:0x0000000685eb88>
が、コンソールでClient.last.state_changes
を実行するときには、正常に動作します:
私は私はこのエラーが出るClient.current_state(Client.last)
でそれをテストするとき、レールコンソールでただしcurrent_state
を返すメソッドを定義しています。
マイclient.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のクラスとインスタンスメソッド](http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/)をお読みになることをお勧めします –