I’m trying to create a list of recently changed objects…
but…
@events = Event.find :all, :limit => 2, :order => ‘updated_at desc’
@videos = Video.find :all, :limit => 2, :order => ‘updated_at desc’
@recently = [@events, @videos].sort_by(&:updated_at).reverse
gives a…
undefined method `updated_at’ for #Array:0x24ecf30
Any ideas of best practice for this type of thing?
On 17 Aug 2008, at 22:28, kevin evans wrote:
undefined method `updated_at’ for #Array:0x24ecf30
that’s because the elements of the array you are sorting are the 2
arrays you fetched previously
You want something like
(@events + @videos).sort_by(&:updated_at).reverse
Fred
On Mon, Aug 18, 2008 at 12:13 AM, Xavier N. [email protected] wrote:
def recent
def self.recent
Thanks, that works great.
On Aug 17, 10:58 pm, Frederick C. [email protected]
On Sun, Aug 17, 2008 at 11:28 PM, kevin evans [email protected] wrote:
I’m trying to create a list of recently changed objects…
but…
@events = Event.find :all, :limit => 2, :order => ‘updated_at desc’
@videos = Video.find :all, :limit => 2, :order => ‘updated_at desc’
@recently = [@events, @videos].sort_by(&:updated_at).reverse
Fred already explained the problem.
Let me add a remark: that code looks like something that belongs to
some controller. If that’s the case, in a controller finds like those
are better encapsulated in the models, using named_scope in recent
Rails, or a regular method in previous ones:
class Event
…
def recent
find :all, :limit => 2, :order => ‘updated_at desc’
end
end
so that the controller just says:
@recently = (Event.recent +
Videos.recent).sort_by(&:updated_at).reverse
– fxn
Thanks, yes, you are right.