Forum: Ruby on Rails RE: Left Joins with Rails?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
60e38de043848f82392062088f191213?d=identicon&s=25 Hogan, Brian P. (Guest)
on 2006-03-02 20:21
(Received via mailing list)
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
0df56f2edb0d80e81c5c0ef1ca91690e?d=identicon&s=25 Joel Oliveira (Guest)
on 2006-03-02 21:43
(Received via mailing list)
Boy am I an idiot!

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

- Joel
This topic is locked and can not be replied to.