RE: Left Joins with Rails?


Easy, but you’re confusing me a bit with your models… You have n
apartment table, but you reference Listing in the googlemap model and in
the controller. Also, you’re a little backwards there on your joins.

If you did this:

class Apartment < ActiveRecord::Base
	belongs_to :googlemap # because apartments HAS THE


class Googlemap < ActiveRecord::Base
	has_one :listing

Then you can do this:

def results
@apartments = Apartment.find(:all,
:include =>[:googlemap]
:conditions => [“bedrooms=? AND bathrooms=?”, :params])

Or do it like you do now.
When you reference @apartment.googlemap.url, your data will be found.
However, if you use the above method, then the data will be fetched all
at once, as opposed to only upon request.

See “eager loading of associations” in the manual.

*** Also, I don’t understand your data model, but I would probably avoid
the whole mess and put the field on the apartments table… Unless
googlemaps table will have more columns than just the map URL.



Boy am I an idiot!

Thanks guys - I finally see where I was going wrong.

  • Joel