Ruby on Rails

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsWeb Design & DevelopmentRuby on Rails

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old January 23rd, 2012, 02:38 PM
clem_c_rock clem_c_rock is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 33 clem_c_rock New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 3 h 33 m 46 sec
Reputation Power: 0
One to many query for 3 tables

Hello,
I am looking for a join query between 3 tables that will give me the results I'm looking for.

I have a properties tables that has many layouts and many photos as well. The layouts table can also, have many layouts. When I run the query, I want to show a list of properties w/ one main photo from the photos table, and a list of layouts per property. Right now, the query I have will show a list of properties, but properties will be displayed multiple times if there are more than one photos. Here's my current query in rails form:

Code:
    @properties = Property.paginate(:page => params[:page], 
                                      :select => ["properties.*, layouts.*, photos.*"],
                                        :joins => ["INNER JOIN layouts ON layouts.property_id = properties.property_id LEFT JOIN photos ON photos.property_id = properties.property_id"],
                                          :per_page => 20) 


or, in raw sql:

Code:
SELECT properties.*, layouts.*, photos.photo_file_name FROM `properties` INNER JOIN layouts ON layouts.property_id = properties.property_id LEFT JOIN photos ON photos.property_id = properties.property_id WHERE (properties.property_status='available') ORDER BY layouts.rent LIMIT 20 OFFSET 0


Any help would be greatly appreciated!

Reply With Quote
  #2  
Old January 25th, 2012, 07:35 AM
clem_c_rock clem_c_rock is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 33 clem_c_rock New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 3 h 33 m 46 sec
Reputation Power: 0
found the solution:

@properties = Property.paginate(:page => params[:page], :per_page => 20).includes(:photos).includes(:layouts).order("properties.modify_date, layouts.modify_date").all



Cheers!

Reply With Quote
Reply

Viewing: Tutorialized ForumsWeb Design & DevelopmentRuby on Rails > One to many query for 3 tables


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap