Expiring messages is certainly business logic so it should go in the
model not the controller. You could put the code in an after_find
callback in the model so it is run every time a record is fetched.
However if the decision as to whether a message is expired or not is
entirely based on the time since creation there may be no need for a
column in the database. Simple add a method to the model, something
like
def expired?
Time.now - self.created_at > 10.days # or whatever the period is
end
Colin
fwiw, I prefer to write that sort of condition as:
=====
def expired?
self.created_at < 10.days.ago
end
=====
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/
> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
Yes, much better, I had forgotten about ago.
Colin
I presume here you are trying to get round the problem that if a find
is performed with a condition of !expired then the find should somehow
reject the record after setting it to expired. I had not thought of
that complication. In particular if the find is for a set of records
then the changed records must be removed from the collection. I don't
know how to do that.
Any suggestions from the more experienced here?
Colin
>
> On Jun 20, 6:53 am, Colin Law <clan...@googlemail.com> wrote:
>> On 20 June 2011 09:15, John Feminella <jo...@bitsbuilder.com> wrote:
>>
>> >> def expired?
>> >> Time.now - self.created_at > 10.days # or whatever the period is
>> >> end
>>
>> > fwiw, I prefer to write that sort of condition as:
>>
>> > =====
>> > def expired?
>> > self.created_at < 10.days.ago
>> > end
>> > =====
>>
>> Yes, much better, I had forgotten about ago.
>>
>> Colin
>
I tend to agree, it is much simpler to do it this way rather than
having an explicit status. It is also poor database design as you
have the same information stored twice in the database (once in the
explicit status and once in created_at). If you need an explicit
status then just provide a method called expired which returns the
state based on created_at. To the code calling this it is virtually
indistinguishable from a value stored in the database.
Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/9S1lx20_7JoJ.
Bingo!
Although given what he seems to be attempting here I would make it the default
scope:
scope :default, lambda {where(["created_at > ?", 10.days.ago])}
If you want to search for expired messages, just add another scope:
scope :expired, lambda {where(["created_at < ?", 10.days.ago])}
I am not sure about that, but have not got time to test it now. If
you have a default scope and then add another will not the :expired
scope be applied *as well as* the default, resulting in no records
found at all.
Also, even if the above does work I think one of the scopes should
include the exact equality, otherwise records that are exactly
10.days.ago will not be found by either (not that there will be many
as it must be exact to the second I think).
Colin