# Best way to display recently changed objects?

**URL:** <https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104>\
**Category:** Rails\
**Created:** [August 17, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104 "2008-08-17T23:29:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [August 17, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104/1 "2008-08-17T23:29:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [August 17, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104/2 "2008-08-17T23:58:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [August 18, 2008, 12:15am UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104/3 "2008-08-18T00:15:22Z")

</div>

On Mon, Aug 18, 2008 at 12:13 AM, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def recent
> 
> ```

def self.recent

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [August 18, 2008, 12:15am UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104/4 "2008-08-18T00:15:57Z")

</div>

Thanks, that works great.

On Aug 17, 10:58 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [August 18, 2008, 12:13am UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104/5 "2008-08-18T00:13:37Z")

</div>

On Sun, Aug 17, 2008 at 11:28 PM, kevin evans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [August 18, 2008, 12:29am UTC](https://www.ruby-forum.com/t/best-way-to-display-recently-changed-objects/146104/6 "2008-08-18T00:29:25Z")

</div>

Thanks, yes, you are right.
