Joins parent and child tables in rails
1. Get parent objects:
Alert.joins("LEFT JOIN alert_events on alert_events.alert_id = alerts.id
").group('alerts.id').select('
2. Get Child Object:
AlertEvent.joins("INNER JOIN alerts on alerts.id = alert_events.alert_id
").group('alert_events.id').se
Active record for child object
AlertEvent.joins(:alert).where
3. Joins multiple tables:
@previous_seafood_items = SeafoodItem.joins("LEFT JOIN auction_items on auction_items.itemable_id = seafood_items.id and auction_items.itemable_type = 'SeafoodItem' and auction_items.auction_id is NULL")
.group('seafood_items.id')
.select('seafood_items.*')
.where(user_id: current_user.id )
.paginate(:page => params[:page], :per_page => 20)
Comments
Post a Comment