# Possible to run only one query?

**URL:** <https://www.ruby-forum.com/t/possible-to-run-only-one-query/124111>\
**Category:** Rails\
**Created:** [December 24, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/possible-to-run-only-one-query/124111 "2007-12-24T21:14:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [December 24, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/possible-to-run-only-one-query/124111/1 "2007-12-24T21:14:49Z")

</div>

Hi all,

I’m hoping that someone out there knows a more efficient way to run a  
query on a model with multiple levels of belongs\_to’s. I’m working on  
a property management application, and the models I have are  
Properties, Units, and Leases.

Property —(has\_many)–\> Units  
Units \<-- (has\_and\_belongs\_to\_many) --\> Leases

Leases have two date fields: starts\_on, and ends\_on, as well as an  
integer field called square\_feet.

My goal is to figure out the percentage of a property that is leased.  
I’m currently doing it with a couple of functions in the property and  
unit models. These are:

PROPERTY.RB:

* * *

```
 99 # Returns the percentage of a property that is leased
100 def percentage_leased
101 unleased_footage = 0.0
102 leased_footage = 0.0
103 for unit in self.units
104 if unit.leased
105 leased_footage += unit.square_feet
106 else
107 unleased_footage += unit.square_feet
108 end
109 end
110
111 if unleased_footage == 0
112 return 100.0
113 else
114 return (leased_footage / self.total_area) * 100
115 end
116 end # of function percentage_leased

```

UNIT.RB:

* * *

```
  7 # Returns TRUE if the unit is leased, FALSE if it is vacant
  8 def leased
  9 results = self.leases.find(:all,
 10 :conditions => "start_date <=

```

NOW() AND end\_date \>= NOW()")  
11 if results.size != 1  
12 return FALSE  
13 else  
14 return TRUE  
15 end  
16 end

In the end, calling property.percentage\_leased generates _lots_ of  
queries on larger properties. Is there a way to do this with only one  
query? Or somehow more efficiently???

Thanks!

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [December 24, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/possible-to-run-only-one-query/124111/2 "2007-12-24T21:36:28Z")

</div>

On Dec 24, 2007, at 2:14 PM, Neal L wrote:

> ```
> 13 else
> 14 return TRUE
> 15 end
> 16 end
> 
> ```
> 
> In the end, calling property.percentage\_leased generates _lots_ of  
> queries on larger properties. Is there a way to do this with only one  
> query? Or somehow more efficiently???

Take a look into :include, which tells ActiveRecord to preload the  
specified association(s). Something like

results = self.leases.find(:all, :conditions =\> [], :include =\>  
[])

Peace,  
Phillip
