I’m using Ruby on Rails 3 and I created a few scopes in my object class but when I call them from within my code it returns an error:
irb>Transaction.first.committed
=> undefined method `commited’ for #
object class:
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
You can’t call a scope (a class method) on a single Transaction object (an instance).
You would have to do this:
You get back an
ActiveRelation(essentially anArray, but you can call other scopes on it).What would you expect
Transaction.first.committedto do, anyways? You’d have a single object, and then you’d try to find where itstransaction_typeis “committed”. You already have the object, so you would call its#transaction_typemethod.A scope would bring back all Transaction objects that have a transaction type of committed. If you want an instance method that tells you if a single object is committed, then you’d have to make an instance method like:
Then you could write: