2012-07-05 3 views
1

작동하지 않습니다 그리고 내 객체 클래스에 몇 가지 범위를 생성하지만 난 내 코드 내에서 호출 할 때 오류 반환 :범위 선언은 레일 3에 내가 루비를 사용하고

irb>Transaction.first.committed

=> undefined method `commited' for #

객체 클래스를 :

class Transaction < ActiveRecord::Base

attr_accessible :amount, :description, :published, :task_description_id, :discrete_task_id, :transaction_type 

belongs_to :discrete_task 

scope :committed, where(:transaction_type => "committed") 

scope :obligated, where(:transaction_type => "obligated") 

scope :expensed, where(:transaction_type => "expensed") 

end

답변

0

단일 트랜잭션 개체 (인스턴스)에 대해 범위 (클래스 메서드)를 호출 할 수 없습니다.

Transaction.committed 

당신은 되돌려 ActiveRelation 수 (본질적으로 Array,하지만 당신은 다른 범위를 호출 할 수 있습니다) :

이 작업을 수행해야합니다.

어쨌든 Transaction.first.committed에 무엇을 기대 하시겠습니까? 당신은 하나의 객체를 가질 것이고, 그 다음에 transaction_type이 "저지른"곳을 찾으려고 할 것입니다. 이미 객체가 있으므로 #transaction_type 메서드를 호출합니다.

범위는 모두 트랜잭션 유형이 커밋 된 트랜잭션 개체를 반환합니다.

def committed? 
    transaction_type == "committed" 
end 

그럼 당신은 쓸 수 있습니다 : 당신이 하나 객체가 최선을 다하고 있는지를 알려주는 예를 방법을 원하는 경우에, 당신은 같은 인스턴스 메서드 만들해야 할 것

Transaction.first.committed? # => true 
+0

도움 주셔서 감사합니다. –

0

Transaction.firstTransaction 개체를 반환하므로 where을 호출 할 수 없습니다. 시도 :

Transaction.committed.first 
관련 문제