# Activerecord :include

**URL:** <https://www.ruby-forum.com/t/activerecord-include/151150>\
**Category:** Rails\
**Created:** [October 17, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/activerecord-include/151150 "2008-10-17T12:27:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [October 17, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/activerecord-include/151150/1 "2008-10-17T12:27:59Z")

</div>

basically I’ve got a design which has a model describing, say a  
folder, and within each folder I associate some notes. I’ve built my  
models so that a folder can have many notes, and that a note belongs  
to a folder. Now, a folder can belongs to a person, and a person  
therefore has many folders. what I’m attempting to do is display all  
the folders for a given user, which is pretty simple as I can simply  
so

folders = Person.find(1,:include=\>[:folders])

however, what I also want to do is display the most recent note  
against each folder. Although I can do this by simply adding the  
notes into my include,

folders = Person.find(1,:include=\>[:folders=\>:notes])

I’d really perfer not to load each and every note assigned to each  
and every folder. I’ve been playing about for a while now trying to  
get active record to load my assoicates but only include the first  
note for each folder. can anybody help? maybe it’s not even  
possible? Perhaps I need to change my approach? Any help/advice  
would be greatly appreciated/welcome.

thanks

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [October 17, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/activerecord-include/151150/2 "2008-10-17T12:42:43Z")

</div>

On 17 Oct 2008, at 11:27, hiddenhippo wrote:

> get active record to load my assoicates but only include the first  
> note for each folder. can anybody help? maybe it’s not even  
> possible? Perhaps I need to change my approach? Any help/advice  
> would be greatly appreciated/welcome.

:include can’t do that - it’s fundamentally hard from it’s point of  
view to only fetch the first record of some collection

Fred

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [October 18, 2008, 1:33am UTC](https://www.ruby-forum.com/t/activerecord-include/151150/3 "2008-10-18T01:33:39Z")

</div>

hiddenhippo wrote:

> however, what I also want to do is display the most recent note  
> would be greatly appreciated/welcome.  
> This may work:

class Folder \< ActiveRecord::Base  
has\_one :most\_recent\_note, :class\_name =\> ‘Note’,  
:foreign\_key =\> :note\_id, :conditions =\>  
‘notes.created\_at = (select max(created\_at) from notes)’  
end

folders = Person.find(1, :include =\> {:folders =\> :most\_recent\_note})

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [October 18, 2008, 1:57am UTC](https://www.ruby-forum.com/t/activerecord-include/151150/4 "2008-10-18T01:57:37Z")

</div>

Mark J. wrote:

> This may work:
> 
> class Folder \< ActiveRecord::Base  
> has\_one :most\_recent\_note, :class\_name =\> ‘Note’,  
> :foreign\_key =\> :note\_id, :conditions =\>  
> ‘notes.created\_at = (select max(created\_at) from notes)’  
> end
> 
> folders = Person.find(1, :include =\> {:folders =\> :most\_recent\_note})

Sorry, didn’t think that one through properly. How about:

has\_one :most\_recent\_note, :class\_name =\> ‘Note’, :conditions =\> \<\<END  
notes.id = (select max(id) from notes where notes.folder\_id =  
folders.id)  
END

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
