John-Mason P. Shackelford
2011-Jan-13 17:15 UTC
Rails 3, SQL for Association - enabling a union, Arel?
In the class below I have a method which returns the value of two associations concatenated (#configured_resources), e.g. the union is done via ruby instead of via SQL. The issue is that I don''t get a proxy back which messes up a subsequent call #to_xml(include: :my_concat_association). So it seems the better way is issue a SQL query with a UNION but I''d rather not write out the association SQL by hand--seems more logical to have a method something like #association_union( :assoc1, :assoc2) which would read the association SQL from the two associations I have defined and produce the appropriate union statement. Trouble is I can''t figure out how to get the SQL out of the association. I''ve looked at the AssociationCollection and other proxies, but it isn''t adding up for me. Any pointer would be helpful. class ApplicationInstance < ActiveRecord::Base belongs_to :packaged_application # http://guides.rubyonrails.org/association_basics.html#the-has_many-association # http://my.safaribooksonline.com/book/web-development/ruby/9780132480345/advanced-active-record/ch09lev1sec7 has_many :application_resources has_many :schema_resource_sets, :through => :application_resources, :source => :configurable, :source_type => ''SchemaResourceSet'' has_many :example_resources, :through => :application_resources, :source => :configurable, :source_type => ''ExampleResource'' def configured_resources # schema_resource_sets: # SELECT "schema_resource_sets".* # FROM "schema_resource_sets" # INNER JOIN "application_resources" # ON "application_resources".configurable_id "schema_resource_sets".id # AND "application_resources".configurable_type ''SchemaResourceSet'' # WHERE (("application_resources".application_instance_id = 1)) schema_resource_sets + example_resources end end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.