scope determined by model function call

Hi All,

I can work around this if it isn't possible, but I am looking to
create a scope for a model which returns records based on the result
of a model function.

Essentially, I have the following:

class contract
  has_many contract_statuses

  def current_status(date)
    ....
  end
end

class contract_status
  belongs_to :contract
  belongs_to :status
end

class status
  has_many :contract_statuses
end

The contract_statuses are there to track the status of a contract over
time with the latest being the current status for a particular
contract. A 'status' simply has a 'name' field.

In my contract model, i have a current_status function which returns
the contract_status record that was relevant as a particular time,
given a date parameter.

The scope I would like would be something like contracts.active, where
active relates to the name of the current status. Ideally, I would
like to get the status through the current_status method of the
contract, but I've not been able to find a way to do this in a
scope.....

Is this possible?

Don't suppose anyone has any thoughts regarding this?

Thanks

Paul

I probably wouldn’t go as far as you have in breaking status out into a separate model. I’d keep it as an attribute of contract and use ActiveModel::Dirty along with an ActiveRecord after_save callback to handle updates into a contract_status_changes table.

Alternately, you could use something like AASM to implement a finite state machine and add rows to the history table using events / transitions.