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
--
Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Aug-11 22:13 UTC
Re: Best Practicies: Combining Polymorphic Results?
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_codeIs there any reason that you can''t just do VendorCode.all ? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.