RE: Left Joins with Rails?


#1

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

FOREIGN KEY COLUMN
end

class Googlemap < ActiveRecord::Base
	has_one :listing
end

Then you can do this:

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

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.

-bph


#2

Boy am I an idiot!

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

  • Joel