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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Don''t suppose anyone has any thoughts regarding this? Thanks Paul On Jul 27, 12:19 pm, paulo <paul.a.taylo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Fri, Jul 29, 2011 at 10:56 AM, paulo <paul.a.taylor84-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Don''t suppose anyone has any thoughts regarding this? > > > Thanks > > > Paul > > On Jul 27, 12:19 pm, paulo <paul.a.taylo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.