I had the same problem (and opinion), I wrote a plugin last year to
solve this issue, and  called it virtual aliases.

I solved two issues with it:

1. make :select work even when eager loading (:include ); you can thus
limit the fields that are requested in the query ; objects are created
but fields not requested are not present in the object.
2. be able to specify conditions/order/group in a way that you can
access relations on ActiveRecord.

To extend your example (added country, and creator)

 class Person < ActiveRecord::Base
   # Everybody has a home address
   belongs_to :home_address,:class_name  => 'Address', :foreign_key =>
'home_address_id'

   # Everybody has a work address
   belongs_to :work_address,  :class_name  => 'Address', :foreign_key
=> 'work_address_id'

  # Only some people have a second work address
   belongs_to :work_address_2, :class_name  => 'Address', :foreign_key
=> 'work_address_2_id'
   belongs_to :creator, : class_name => 'User'
 end

 class Address < ActiveRecord::Base
   belongs_to :creator, : class_name => 'User'
  belongs_to :country
   # street_no (string)
   # street_name (string)
end

class Country
   belongs_to :creator, : class_name => 'User'
  # name
end

These will all work and do the necessary joins automatically :

Person.find(:all, :include => :valiases,  :conditions =>
"{work_address.country.name} = 'Belgium' ", :order =>
"{home_address.street_name}" )
Person.find(:all, :include => :valiases,  :conditions =>
"{work_address.creator.name} = 'bduc ", :order =>
"{home_address.country.creator.name}" )

All valiases are placed between curly braces and replaced by the
correct real aliases activerecord makes up for then.

Currently used and tested on rails 2.2

The code is at dev.dyndaco.com ; but you cannot checkout the code yet;
only view it (will make checkout available later this month).
There currently is not much documentation and there are no tests
included; that's way I don't make to much fuss about it.
Apart from that , it works great !

If you're intrested let me know.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to