# Best Practicies: Combining Polymorphic Results?

**URL:** <https://www.ruby-forum.com/t/best-practicies-combining-polymorphic-results/173606>\
**Category:** Rails\
**Created:** [August 11, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/best-practicies-combining-polymorphic-results/173606 "2009-08-11T23:02:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [August 11, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/best-practicies-combining-polymorphic-results/173606/1 "2009-08-11T23:02:15Z")

</div>

Hi,

I am trying to combine results on two related associations and was  
wondering if there were a best practicies way to do this.

I basically have VendorCodes that are matched to both Vendors and  
VendorStaff.

vendors have VendorCodes (polymorphic)  
vendorStaffs have VendoorCodes (polymorphic)

I want to easily be able to derive something like:  
all\_vendor\_codes = vendor.vendor\_codes +  
vendor.vendor\_staffs.collect.vendor\_code

I have some models set up as, is there a better way to drive this other  
than the way I’ve done it? Thanks for any help!

My Model:

class Vendor \< ActiveRecord::Base  
has\_many :vendor\_staffs  
has\_many :vendor\_codes, :as =\> :vendor\_codable  
end

class VendorStaff \< ActiveRecord::Base  
belongs\_to :vendors  
has\_many :vendor\_codes, :as =\> :vendor\_codable  
end

class VendorCode \< ActiveRecord::Base  
belongs\_to :vendor\_codable, :polymorphic =\> true  
end

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [August 12, 2009, 12:13am UTC](https://www.ruby-forum.com/t/best-practicies-combining-polymorphic-results/173606/2 "2009-08-12T00:13:43Z")

</div>

Mario Gr wrote:

> Hi,
> 
> I am trying to combine results on two related associations and was  
> wondering if there were a best practicies way to do this.
> 
> I basically have VendorCodes that are matched to both Vendors and  
> VendorStaff.
> 
> vendors have VendorCodes (polymorphic)  
> vendorStaffs have VendoorCodes (polymorphic)
> 
> I want to easily be able to derive something like:  
> all\_vendor\_codes = vendor.vendor\_codes +  
> vendor.vendor\_staffs.collect.vendor\_code

Is there any reason that you can’t just do VendorCode.all ?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
