rails 3 group_by for simple active record groupings

Posted on 2010-11-21
Last Modified: 2012-08-14
so i have fixtures that have different seasons, i want to group and display the fixtures by season. On the fixtures index page i want the current season displayed and then link to old seasons. I was thinks some kind of group_by method?
class FixturesController < ApplicationController


  def index

    @fixtures = Fixture.all




<h1>Listing fixtures</h1>

<% @fixtures.each do |fixture| %>

  <%= fixture.season.title %></td>

  <td><%= fixture.event.title %></td>

  <td><%= %></td>

  <td><%= %></td>

  <td><%= %></td>

class Season < ActiveRecord::Base

  has_many :fixtures


class Fixture < ActiveRecord::Base

  belongs_to :season

  belongs_to :event

  belongs_to :home_team, :class_name => 'Team'

  belongs_to :away_team, :class_name => 'Team'


Open in new window

Question by:depassion
  • 2

Expert Comment

ID: 34425381
I don't know how you are getting the @fixtures in your index action ... anyway I believe you just need to order by season
something like:

@fixtures = Fixture.all(:order=>"season_id ASC")

Accepted Solution

depassion earned 0 total points
ID: 34426260
i"ve solved it by creating a get_season method which works out the current season.

before_filter :get_season

def get_season

    @current_season = Season.where(["start_date < ? AND end_date > ?",,]).first



<% @current_season.fixtures.each do |fixture| %>



Open in new window


Author Closing Comment

ID: 34447401
took too long for a solution from the community so was forced to pay to get help

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
no images after rails 3.1 deploy 7 221
Ruby Gems Passenger Error 13 1,057
Github help 2 446
AES-256-CBC Encryption between Ruby and PHP 5 468
I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now