# ActiveRecord and :include

**URL:** <https://www.ruby-forum.com/t/activerecord-and-include/165469>\
**Category:** Rails\
**Created:** [April 14, 2009, 9:08am UTC](https://www.ruby-forum.com/t/activerecord-and-include/165469 "2009-04-14T09:08:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@slava](https://www.ruby-forum.com/u/slava)\
**Post date:** [April 14, 2009, 9:08am UTC](https://www.ruby-forum.com/t/activerecord-and-include/165469/1 "2009-04-14T09:08:56Z")

</div>

here is my model

class ProductLocation \< ActiveRecord::Base  
belongs\_to :property  
has\_many :product\_location\_mappings  
end

I want to load product\_locations along with  
referenced :product\_location\_mappings and return a deep model.

```
@product_locations = ProductLocation.find_all_by_property_id

```

(property\_id, :include =\> [:property, :product\_location\_mappings])

however result does not include :product\_location\_mappings…  
I am using rails 2.3.2, not sure what to do…

---

<div class="post-metadata">

**Author:** ![slava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@slava](https://www.ruby-forum.com/u/slava)\
**Post date:** [April 14, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/activerecord-and-include/165469/2 "2009-04-14T13:55:41Z")

</div>

slava wrote:

> here is my model
> 
> class ProductLocation \< ActiveRecord::Base  
> belongs\_to :property  
> has\_many :product\_location\_mappings

Tip: Model names shouldn’t be redundant with their navigation. Just call  
it  
Mappings. An even better name would reveal the intent of the model…

> I am using rails 2.3.2, not sure what to do…  
> Show a unit test that includes all your models linked together, per  
> their yml  
> fixture files. Then show that find() statement locating your target  
> record.

If the code still does not work, gem install assert2  
assert\_efficient\_sql,  
require ‘assert\_efficient\_sql’, and wrap this around your test:

inspect\_sql :verbose =\> true do  
product\_locations = ProductLocation.find\_all\_by\_property\_id  
(property\_id, :include =\> [:property,  
:product\_location\_mappings])  
end

BTW you might also have read your log/test.log, but inspecting your code  
is what  
tests are for…

You will get a big blob of JOIN statements. Inspect them to see if they  
make  
sense, and run them in your database directly to see what they do.

But, in general, your code looks like the :include directives I have  
written, so  
I don’t see any specific problem with it. That’s why the shotgun of  
recommendations.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![slava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@slava](https://www.ruby-forum.com/u/slava)\
**Post date:** [April 14, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/activerecord-and-include/165469/3 "2009-04-14T20:14:06Z")

</div>

thanks,  
I ended up doing this.

```
  format.json { render :json => @product_locations.to_json

```

(:include =\> :product\_location\_mappings)}
